summary refs log tree commit diff stats
path: root/day19.py
diff options
context:
space:
mode:
Diffstat (limited to 'day19.py')
-rw-r--r--day19.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/day19.py b/day19.py
new file mode 100644
index 0000000..d7b92ee
--- /dev/null
+++ b/day19.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+import re
+from collections import defaultdict
+
+replacements = []
+starter = None
+with open('day19.txt') as data:
+    for line in data:
+        groups = re.search(r'(\w+) => (\w+)', line)
+        if groups:
+            replacements.append(groups.groups((1, 2)))
+        else:
+            if line != '\n':
+                starter = line.strip()
+
+# part 1
+compounds = set()
+for elem, repl in replacements:
+    for i in range(len(starter)):
+        if starter[i:i+len(elem)] == elem:
+            compound = starter[:i] + repl + starter[i+len(elem):]
+            compounds.add(compound)
+print(len(compounds))
+
+# part 2
+elements = [s for s in re.split(r'([A-Z][^A-Z]*)', starter) if s]
+print(len(elements) - elements.count('Ar') - elements.count('Rn') - 2 * elements.count('Y') - 1)