2
1
Fork 0
aoc2021/05/solution_2.c

34 lines
920 B
C
Raw Normal View History

2021-12-05 09:05:55 +01:00
#include <stdio.h>
2021-12-06 00:31:09 +01:00
#define put(x, y) intersections += ++map[x][y] == 2
2021-12-05 09:05:55 +01:00
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 incY = y0 < y1 ? 1 : -1;
char incX = x0 < x1 ? 1 : -1;
if (x0 == x1) {
2021-12-06 00:31:09 +01:00
for (unsigned y = y0; y != y1; y += incY)
put(x0, y);
put(x0, y1);
2021-12-05 09:05:55 +01:00
} else if (y0 == y1) {
2021-12-06 00:31:09 +01:00
for (unsigned x = x0; x != x1; x += incX)
put(x, y0);
put(x1, y0);
2021-12-05 09:05:55 +01:00
} else {
unsigned x = x0, y = y0;
while (x != x1 && y != y1) {
2021-12-06 00:31:09 +01:00
put(x, y);
2021-12-05 09:05:55 +01:00
x += incX;
y += incY;
}
2021-12-06 00:31:09 +01:00
put(x, y);
2021-12-05 09:05:55 +01:00
}
}
printf("Answer: %d\n", intersections);
}