diff options
Diffstat (limited to '')
-rw-r--r-- | 2021/day05.c | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/2021/day05.c b/2021/day05.c index a51bfc0..93d6dfd 100644 --- a/2021/day05.c +++ b/2021/day05.c @@ -1,22 +1,22 @@ #include <stdio.h> #include <stdlib.h> -static void swap(int *x1, int *y1, int *x2, int *y2) { - int t = *x1; - *x1 = *x2; - *x2 = t; - t = *y1; - *y1 = *y2; - *y2 = t; +static int sign(int x) { + return (x > 0 ? 1 : x < 0 ? -1 : 0); } int main(void) { static int grid[1024][1024]; + static int grid2[1024][1024]; int x1, y1, x2, y2; while (4 == scanf("%d,%d -> %d,%d\n", &x1, &y1, &x2, &y2)) { - if (x1 != x2 && y1 != y2) continue; - if (x2 < x1 || y2 < y1) swap(&x1, &y1, &x2, &y2); - for (int y = y1; y <= y2; ++y) - for (int x = x1; x <= x2; ++x) { - grid[y][x]++; + int dx = sign(x2 - x1); + int dy = sign(y2 - y1); + for ( + int y = y1, x = x1; + (dy > 0 ? y <= y2 : y >= y2) && (dx > 0 ? x <= x2 : x >= x2); + x += dx, y += dy + ) { + if (x1 == x2 || y1 == y2) grid[y][x]++; + grid2[y][x]++; } } int points = 0; @@ -25,4 +25,10 @@ int main(void) { if (grid[y][x] > 1) points++; } printf("%d\n", points); + points = 0; + for (int y = 0; y < 1024; ++y) + for (int x = 0; x < 1024; ++x) { + if (grid2[y][x] > 1) points++; + } + printf("%d\n", points); } |