My solutions to the advent of code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.4 KiB

3 years ago
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void printGrid(unsigned int maxx, unsigned int maxy, unsigned int grid[maxx+1][maxy+1]){
for( unsigned int y = 0; y <= maxy; y++ ){
for( unsigned int x = 0; x <= maxx; x++ ){
printf("%3i", grid[x][y]);
}
printf("\n");
}
}
int main( int argc, char *argv[] ){
if( argc == 2 ) {
FILE *fp=fopen(argv[1], "r");
unsigned int maxx=0, maxy=0;
while (!feof (fp)){
unsigned int x1=0,x2=0,y1=0,y2=0;
fscanf(fp, "%i,%i -> %i,%i\n", &x1,&y1,&x2,&y2);
maxx= MAX( maxx, MAX( x1, x2 ) );
maxy= MAX( maxy, MAX( y1, y2 ) );
}
unsigned int grid[maxx+1][maxy+1];
memset( &grid, 0, (maxx+1)*(maxy+1)*sizeof(int) );
rewind(fp);
unsigned int cellsWithOverlap=0;
while (!feof (fp)){
unsigned int x1=0,x2=0,y1=0,y2=0;
fscanf(fp, "%i,%i -> %i,%i\n", &x1,&y1,&x2,&y2);
if ( x1 == x2 ){
for( unsigned int y = MIN(y1,y2); y<=MAX(y1,y2); y++ )
if ( 1 == grid[x1][y]++) cellsWithOverlap++;
}
if ( y1 == y2 ){
for( unsigned int x = MIN(x1,x2); x<=MAX(x1,x2); x++ )
if ( 1 == grid[x][y1]++) cellsWithOverlap++;
}
}
printf("%i cells with overlap\n", cellsWithOverlap);
fclose(fp);
return 0;
} else {
printf("You need to provide a file\n");
return 1;
}
}