summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-11 14:11:49 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-11 14:11:49 -0800
commit04d85bf194d50f8da584a8c657d7490649befb7c (patch)
tree7b8389084e8c4163b6018b709a937d1a06545fd5
parentf9e4204dd9b107174ad2d6f31c7a5068b2fe40da (diff)
downloadAdventOfCode2021-04d85bf194d50f8da584a8c657d7490649befb7c.tar.gz
solution for day 11
-rw-r--r--day11.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/day11.py b/day11.py
new file mode 100644
index 0000000..e939ed0
--- /dev/null
+++ b/day11.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+
+import numpy as np
+from scipy.ndimage import convolve
+
+with open("day11.txt") as data:
+    octopi = np.array([list(map(int, line.strip())) for line in data])
+
+neighbors = np.ones((3,3), dtype=int)
+step = 0
+flashes = 0
+while True:
+    octopi += 1
+    flashed = np.zeros_like(octopi, dtype=bool)
+    while (flashing := ((octopi > 9) & ~flashed)).any():
+        octopi += convolve(flashing.astype(int), neighbors, mode='constant')
+        flashed |= flashing
+
+    octopi[flashed] = 0
+    flashes += flashed.sum()
+    step += 1
+    if step == 100:
+        print(flashes)
+
+    if np.all(octopi == 0):
+        print(step)
+        break