aboutsummaryrefslogtreecommitdiff
path: root/2015/19
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-01 19:15:48 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-01 19:15:48 +0100
commit9a4ea19ae955c86312c62098830a0f0e2994196d (patch)
tree4735be125fc4e1038c310dc724efcafa2339453a /2015/19
parent394df58d6da6928a6a3ebae31362342deca74b48 (diff)
Add day 19 solutions
Diffstat (limited to '2015/19')
-rw-r--r--2015/19/input45
-rwxr-xr-x2015/19/puzzle-1.py30
-rwxr-xr-x2015/19/puzzle-2.sh16
3 files changed, 91 insertions, 0 deletions
diff --git a/2015/19/input b/2015/19/input
new file mode 100644
index 0000000..8bb6fa8
--- /dev/null
+++ b/2015/19/input
@@ -0,0 +1,45 @@
+Al => ThF
+Al => ThRnFAr
+B => BCa
+B => TiB
+B => TiRnFAr
+Ca => CaCa
+Ca => PB
+Ca => PRnFAr
+Ca => SiRnFYFAr
+Ca => SiRnMgAr
+Ca => SiTh
+F => CaF
+F => PMg
+F => SiAl
+H => CRnAlAr
+H => CRnFYFYFAr
+H => CRnFYMgAr
+H => CRnMgYFAr
+H => HCa
+H => NRnFYFAr
+H => NRnMgAr
+H => NTh
+H => OB
+H => ORnFAr
+Mg => BF
+Mg => TiMg
+N => CRnFAr
+N => HSi
+O => CRnFYFAr
+O => CRnMgAr
+O => HP
+O => NRnFAr
+O => OTi
+P => CaP
+P => PTi
+P => SiRnFAr
+Si => CaSi
+Th => ThCa
+Ti => BP
+Ti => TiTi
+e => HF
+e => NAl
+e => OMg
+
+ORnPBPMgArCaCaCaSiThCaCaSiThCaCaPBSiRnFArRnFArCaCaSiThCaCaSiThCaCaCaCaCaCaSiRnFYFArSiRnMgArCaSiRnPTiTiBFYPBFArSiRnCaSiRnTiRnFArSiAlArPTiBPTiRnCaSiAlArCaPTiTiBPMgYFArPTiRnFArSiRnCaCaFArRnCaFArCaSiRnSiRnMgArFYCaSiRnMgArCaCaSiThPRnFArPBCaSiRnMgArCaCaSiThCaSiRnTiMgArFArSiThSiThCaCaSiRnMgArCaCaSiRnFArTiBPTiRnCaSiAlArCaPTiRnFArPBPBCaCaSiThCaPBSiThPRnFArSiThCaSiThCaSiThCaPTiBSiRnFYFArCaCaPRnFArPBCaCaPBSiRnTiRnFArCaPRnFArSiRnCaCaCaSiThCaRnCaFArYCaSiRnFArBCaCaCaSiThFArPBFArCaSiRnFArRnCaCaCaFArSiRnFArTiRnPMgArF
diff --git a/2015/19/puzzle-1.py b/2015/19/puzzle-1.py
new file mode 100755
index 0000000..8527ca0
--- /dev/null
+++ b/2015/19/puzzle-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+import re
+
+def main() -> None:
+ replacements: dict[str, str] = {}
+ with open("input", "r", encoding="utf-8") as f:
+ for line in f.readlines():
+ line = line.strip()
+ if line != "":
+ parts = line.split(" => ")
+ if len(parts) == 2:
+ while parts[0] in replacements:
+ parts[0] += "_"
+ replacements[parts[0]] = parts[1]
+ else:
+ molecule = line
+
+ unique_molecules: set[str] = set()
+ for pattern in replacements:
+ trim_pattern = pattern.replace("_", "")
+ for match in re.finditer(trim_pattern, molecule):
+ before = molecule[:match.start()]
+ after = molecule[match.start():].replace(trim_pattern, replacements[pattern], 1)
+ unique_molecules.add(before + after)
+
+ print(len(unique_molecules))
+
+if __name__ == "__main__":
+ main()
diff --git a/2015/19/puzzle-2.sh b/2015/19/puzzle-2.sh
new file mode 100755
index 0000000..78c3ce5
--- /dev/null
+++ b/2015/19/puzzle-2.sh
@@ -0,0 +1,16 @@
+#!/usr/bin/env sh
+
+# I could not figure this out on my own, but god bless this guy for doing the math:
+# https://www.reddit.com/r/adventofcode/comments/3xflz8/day_19_solutions/cy4etju/
+
+# Some people has inputs that could be solved by just going in reverse trivially, my input did not
+# work
+
+sed -n '
+$ {
+ s/^/0/
+ s/\(Ar\|Rn\)//g
+ s/\(Y\|$\)/-1/g
+ s/[A-Z][a-z]*/+1/g
+ p
+}' input | bc