summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-24 23:04:29 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-24 23:04:29 -0800
commitbab038a16a7bf6265c8442fa3c184efc7d48badc (patch)
tree8bf99af7fbe4db59b11db67d3fbf3d4d697f8688
parent47a63a242fcbeba4fe7d0a52c4902ba36a67df8b (diff)
downloadAdventOfCode2021-bab038a16a7bf6265c8442fa3c184efc7d48badc.tar.gz
solution for day 25 HEAD master
-rw-r--r--day25.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/day25.py b/day25.py
new file mode 100644
index 0000000..3d1bcb1
--- /dev/null
+++ b/day25.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+import numpy as np
+with open('day25.txt') as data:
+    to_num = {
+        '.': 0,
+        '>': 1,
+        'v': 3
+    }
+    cucumbers = np.stack([list(map(lambda x: to_num[x], list(line.strip()))) for line in data])
+
+def shift(line, char):
+    changed = True
+    shifted = np.roll(line, 1)
+    diff = line - shifted
+    if np.count_nonzero(diff == -char) == 0:
+        changed = False
+    line[diff == -char] = char
+    idx = np.nonzero(diff == -char)[0]
+    idx -= 1
+    line[idx] = 0
+    return changed
+
+count = 0
+while True:
+    count += 1
+    changed = False
+    for row in cucumbers[:]:
+        changed |= shift(row, 1)
+    for col in cucumbers.T:
+        changed |= shift(col, 3)
+    if not changed:
+        print(count)
+        break
+