From 554ad695a6aaf0a911157381498a4944da96662d Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sat, 11 Dec 2021 14:05:58 -0500 Subject: Solve day 11 part 1 --- 2021/day11.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 2021/day11.c (limited to '2021') 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 +#include +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); +} -- cgit 1.4.1