2
1
Fork 0
aoc2021/05/solution_1.c
2021-12-05 09:05:55 +01:00

36 lines
910 B
C

#include <stdio.h>
int main(void) {
FILE *fp = fopen("input.txt", "r");
unsigned char map[1000][1000] = {0};
unsigned x0, x1, y0, y1;
unsigned intersections = 0;
while (fscanf(fp, "%u,%u -> %u,%u", &x0, &y0, &x1, &y1) == 4) {
char inc = 1;
if (x0 == x1) {
if (y0 > y1)
inc = -1;
for (unsigned y = y0; y != y1; y += inc) {
map[x0][y]++;
}
map[x0][y1]++;
} else if (y0 == y1) {
if (x0 > x1)
inc = -1;
for (unsigned x = x0; x != x1; x += inc) {
map[x][y0]++;
}
map[x1][y0]++;
}
}
for (unsigned y = 0; y < 1000; y++) {
for (unsigned x = 0; x < 1000; x++) {
if (map[x][y] > 1)
intersections++;
}
}
printf("Answer: %d\n", intersections);
}