summary refs log tree commit diff homepage
path: root/2021/day11.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-12-11 14:05:58 -0500
committerJune McEnroe <june@causal.agency>2021-12-11 14:05:58 -0500
commit554ad695a6aaf0a911157381498a4944da96662d (patch)
tree30ba2aa26ec942d1c6dd4b8cfcd513d12ffac3da /2021/day11.c
parentSolve day 10 part 2 (diff)
downloadaoc-554ad695a6aaf0a911157381498a4944da96662d.tar.gz
aoc-554ad695a6aaf0a911157381498a4944da96662d.zip
Solve day 11 part 1
Diffstat (limited to '2021/day11.c')
-rw-r--r--2021/day11.c46
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);
+}