summary refs log tree commit diff stats
path: root/day18.py
diff options
context:
space:
mode:
Diffstat (limited to 'day18.py')
-rw-r--r--day18.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/day18.py b/day18.py
new file mode 100644
index 0000000..033cc76
--- /dev/null
+++ b/day18.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python
+
+import numpy as np
+from scipy.ndimage import convolve
+
+with open('day18.txt') as data:
+    grid = np.stack([list(map(lambda x: x == '#', line.strip())) for line in data]).astype(int)
+
+
+neighbors = [[1, 1, 1],
+            [1, 0, 1],
+            [1, 1, 1]]
+
+grid1 = np.copy(grid)
+for i in range(100):
+    neighbor_counts = convolve(grid1.astype(int), neighbors, mode='constant', cval=0)
+    survive = grid1 * (neighbor_counts >= 2) * (neighbor_counts <= 3)
+    grid1 = np.where(neighbor_counts == 3, True, survive)
+
+print(np.count_nonzero(grid1))
+
+grid2 = np.copy(grid)
+for coord in {(0,0), (0, 99), (99, 0), (99, 99)}:
+    grid2[coord] = True
+
+for i in range(100):
+    neighbor_counts = convolve(grid2.astype(int), neighbors, mode='constant', cval=0)
+    survive = grid2 * (neighbor_counts >= 2) * (neighbor_counts <= 3)
+    grid2 = np.where(neighbor_counts == 3, True, survive)
+    for coord in {(0,0), (0, 99), (99, 0), (99, 99)}:
+        grid2[coord] = True
+
+print(np.count_nonzero(grid2))