summary refs log tree commit diff stats
path: root/day14.py
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2021-12-19 22:57:37 -0800
committerBrian Chu <brianmchu42@gmail.com>2021-12-19 22:57:37 -0800
commitd4f482804b9777768fa4f9fedde0cd549578502b (patch)
tree211098ceaad6046234e0f55fe86dc539f791d4a4 /day14.py
parent04d85bf194d50f8da584a8c657d7490649befb7c (diff)
downloadAdventOfCode2021-d4f482804b9777768fa4f9fedde0cd549578502b.tar.gz
catch up on solutions up to day 20
Diffstat (limited to 'day14.py')
-rw-r--r--day14.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/day14.py b/day14.py
new file mode 100644
index 0000000..f04d327
--- /dev/null
+++ b/day14.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+from collections import defaultdict
+
+with open('day14.txt') as data:
+    template = next(data).strip()
+    next(data)
+    rules = {}
+    for line in data:
+        k, v = line.strip().split(' -> ')
+        rules[k] = v
+
+def polymerize(steps=10):
+    pairs = defaultdict(int)
+    for i in range(len(template) - 1):
+        pairs[template[i] + template[i+1]] += 1
+
+    for i in range(steps):
+        update = defaultdict(int)
+        for k, v in pairs.items():
+            update[k[0] + rules[k]] += v
+            update[rules[k] + k[1]] += v
+        pairs = update
+
+    counts = defaultdict(int)
+    for k, v in pairs.items():
+        counts[k[0]] += v
+    return max(counts.values()) - min(counts.values()) - 1
+
+print(polymerize())
+print(polymerize(40))