summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-24 19:07:34 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-24 19:07:34 -0800
commit47a63a242fcbeba4fe7d0a52c4902ba36a67df8b (patch)
tree76cdcce2fb435b15ddeed5ee155dfd7c55d73f77
parent605de102a57d01970e7086fdfda50eccc405c0e0 (diff)
downloadAdventOfCode2021-47a63a242fcbeba4fe7d0a52c4902ba36a67df8b.tar.gz
solution for day 24
-rw-r--r--day24.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/day24.py b/day24.py
new file mode 100644
index 0000000..1fad620
--- /dev/null
+++ b/day24.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+with open('day24.txt') as data:
+    program = [tuple(line.strip().split()) for line in data]
+    params = []
+    for i in range(0, 18 * 14, 18):
+        a = int(program[i+4][-1])
+        b = int(program[i+5][-1])
+        c = int(program[i+15][-1])
+        params.append((a, b, c))
+
+
+def f(params, z, w):
+    a, b, c = params
+    if z % 26 + b != w:
+        z = z//a*26 + w + c
+    else:
+        z = z//a
+    return z
+
+zs = {0: [0, 0]}
+for i, p in enumerate(params):
+    new_zs = {}
+    for z, inp in zs.items():
+        for w in range(1, 10):
+            new_z = f(p, z, w)
+            if p[0] == 1 or (p[0] == 26 and new_z < z):
+                if new_z not in new_zs:
+                    new_zs[new_z] = [inp[0]*10+w, inp[1]*10+w]
+                else:
+                    new_zs[new_z][0] = min(new_zs[new_z][0], inp[0]*10+w)
+                    new_zs[new_z][1] = max(new_zs[new_z][1], inp[1]*10+w)
+    print(f"Digit: {i+1} tracked values of z: {len(new_zs)}")
+    zs = new_zs
+print(f'best valid values: {zs[0]}')
+