aboutsummaryrefslogtreecommitdiff
path: root/2021/14
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-14 06:42:36 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-14 06:42:36 +0100
commite9ae9ea8066c57f7065e94d40f510d45da6ad427 (patch)
tree4498c0117ecb27f9caa6be4d64f001ca227af591 /2021/14
parent4f5b004db9a93d9ff356cdd3e6a9fa40421628a7 (diff)
Add day 14 solutions
Diffstat (limited to '2021/14')
-rw-r--r--2021/14/.gitignore1
-rw-r--r--2021/14/Makefile8
-rw-r--r--2021/14/input102
-rw-r--r--2021/14/puzzles.py37
4 files changed, 148 insertions, 0 deletions
diff --git a/2021/14/.gitignore b/2021/14/.gitignore
new file mode 100644
index 0000000..ffc46fe
--- /dev/null
+++ b/2021/14/.gitignore
@@ -0,0 +1 @@
+puzzle-[12].py
diff --git a/2021/14/Makefile b/2021/14/Makefile
new file mode 100644
index 0000000..7c0e609
--- /dev/null
+++ b/2021/14/Makefile
@@ -0,0 +1,8 @@
+all:
+ m4 -DLOOPS=10 puzzles.py >puzzle-1.py
+ m4 -DLOOPS=40 puzzles.py >puzzle-2.py
+ chmod +x puzzle-[12].py
+
+.PHONY: clean run
+clean:
+ rm -f puzzle-[12].py
diff --git a/2021/14/input b/2021/14/input
new file mode 100644
index 0000000..2d09dfd
--- /dev/null
+++ b/2021/14/input
@@ -0,0 +1,102 @@
+SVKVKCCBNHNSOSCCOPOC
+
+KK -> B
+CS -> P
+VV -> O
+KO -> S
+PO -> N
+PH -> K
+BV -> O
+VH -> V
+PF -> P
+HB -> B
+OB -> V
+FC -> F
+OS -> H
+NB -> P
+SH -> S
+KV -> K
+SO -> C
+NP -> B
+NV -> F
+CP -> O
+KS -> N
+FP -> B
+VN -> V
+NC -> S
+FH -> N
+CB -> V
+PV -> B
+NH -> B
+NF -> H
+PC -> B
+NO -> N
+CN -> P
+KF -> B
+VF -> S
+CC -> K
+CF -> N
+PS -> S
+NK -> N
+PB -> H
+BP -> O
+FK -> N
+BO -> S
+OH -> C
+VB -> S
+VP -> F
+FO -> V
+KB -> C
+SK -> H
+CO -> H
+HV -> H
+SV -> B
+BF -> O
+SS -> K
+VK -> S
+HS -> B
+HF -> P
+PK -> F
+BS -> O
+BB -> O
+VC -> P
+OP -> F
+NS -> P
+SB -> C
+NN -> K
+HC -> S
+HH -> B
+FN -> P
+OO -> V
+VO -> N
+ON -> P
+FV -> K
+HK -> S
+FS -> V
+HO -> V
+PN -> B
+KH -> B
+CH -> C
+KP -> S
+BH -> O
+BK -> B
+FB -> H
+VS -> S
+HP -> O
+SP -> P
+OV -> F
+OF -> H
+OC -> V
+KN -> H
+BC -> F
+BN -> F
+CK -> K
+SN -> P
+SF -> K
+KC -> C
+SC -> C
+HN -> V
+OK -> O
+FF -> V
+CV -> V
+PP -> V
diff --git a/2021/14/puzzles.py b/2021/14/puzzles.py
new file mode 100644
index 0000000..e162ffe
--- /dev/null
+++ b/2021/14/puzzles.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+from collections import Counter
+
+
+def main() -> None:
+ with open("input", "r", encoding="utf-8") as f:
+ formula = f.readline().strip()
+ f.readline()
+
+ data: dict[str, str] = {}
+ for line in f.readlines():
+ x, y = line.strip().split(" -> ")
+ data[(x[0], x[1])] = y
+
+ pairs = Counter(zip(formula, formula[1:]))
+
+ for _ in range(LOOPS):
+ new: Counter[str] = Counter()
+ for pair in pairs:
+ new[(pair[0], data[pair])] += pairs[pair]
+ new[(data[pair], pair[1])] += pairs[pair]
+ pairs = new
+
+ # Ran out of memory, lol
+ # chars = Counter(flatten(map(lambda p: (p[0],) * pairs[p], pairs)))
+
+ chars: Counter[str] = Counter()
+ for pair in pairs:
+ chars[pair[0]] += pairs[pair]
+ chars[formula[-1]] += 1
+
+ print(chars.most_common()[0][1] - chars.most_common()[-1][1])
+
+
+if __name__ == "__main__":
+ main()