diff options
Diffstat (limited to '2021')
-rw-r--r-- | 2021/day11.c | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/2021/day11.c b/2021/day11.c new file mode 100644 index 0000000..d153bf1 --- /dev/null +++ b/2021/day11.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <stdlib.h> +struct Grid { + int e[10][10]; +}; +static int step(struct Grid *next, struct Grid grid) { + for (int y = 0; y < 10; ++y) + for (int x = 0; x < 10; ++x) { + next->e[y][x] = grid.e[y][x] + 1; + } + int flashes = 0; + for (;;) { + int f = 0; + for (int y = 0; y < 10; ++y) + for (int x = 0; x < 10; ++x) { + if (next->e[y][x] < 10) continue; + f++; + next->e[y][x] = 0; + for (int dy = -1; dy < 2; ++dy) + for (int dx = -1; dx < 2; ++dx) { + if (y+dy < 0 || y+dy > 9) continue; + if (x+dx < 0 || x+dx > 9) continue; + if (!next->e[y+dy][x+dx]) continue; + next->e[y+dy][x+dx]++; + } + } + flashes += f; + if (!f) break; + } + return flashes; +} +int main(void) { + size_t cap = 0; + char *buf = NULL; + struct Grid grid = {0}; + for (int y = 0; 0 < getline(&buf, &cap, stdin); ++y) { + for (int x = 0; x < 10; ++x) { + grid.e[y][x] = buf[x] - '0'; + } + } + int flashes = 0; + for (int i = 0; i < 100; ++i) { + flashes += step(&grid, grid); + } + printf("%d\n", flashes); +} |