summary refs log tree commit diff homepage
path: root/2020/day17.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2020/day17.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/2020/day17.c b/2020/day17.c
new file mode 100644
index 0000000..6cab278
--- /dev/null
+++ b/2020/day17.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+struct Dim {
+	char _[32][32][32];
+};
+static int neighbors(const struct Dim *d, int z, int y, int x) {
+	int active = 0;
+	for (int i = -1; i <= +1; ++i)
+	for (int j = -1; j <= +1; ++j)
+	for (int k = -1; k <= +1; ++k) {
+		if (!i && !j && !k) continue;
+		active += (d->_[z+i&31][y+j&31][x+k&31] == '#');
+	}
+	return active;
+}
+static struct Dim step(const struct Dim *prev) {
+	struct Dim next;
+	for (int z = 0; z < 32; ++z)
+	for (int y = 0; y < 32; ++y)
+	for (int x = 0; x < 32; ++x) {
+		int active = neighbors(prev, z, y, x);
+		if (prev->_[z][y][x] == '#') {
+			next._[z][y][x] = (active == 2 || active == 3 ? '#' : '.');
+		} else {
+			next._[z][y][x] = (active == 3 ? '#' : '.');
+		}
+	}
+	return next;
+}
+int main(void) {
+	struct Dim init = {0};
+	for (int y = 0;; ++y) {
+		if (scanf("%s\n", init._[0][y]) < 1) break;
+	}
+	struct Dim prev = init;
+	for (int i = 0; i < 6; ++i) {
+		struct Dim next = step(&prev);
+		prev = next;
+	}
+	int active = 0;
+	for (int z = 0; z < 32; ++z)
+	for (int y = 0; y < 32; ++y)
+	for (int x = 0; x < 32; ++x) {
+		active += (prev._[z][y][x] == '#');
+	}
+	printf("%d\n", active);
+}