From cfa771847548927af7b9ec4374b4bb922c9c0ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 6 Jan 2024 21:35:48 +0100 Subject: [PATCH] 2015 day 19. --- src/holt59/aoc/2015/day19.py | 56 +++++++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day19.txt | 45 +++++++++++++++++ src/holt59/aoc/inputs/tests/2015/day19.txt | 7 +++ 3 files changed, 108 insertions(+) create mode 100644 src/holt59/aoc/2015/day19.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day19.txt create mode 100644 src/holt59/aoc/inputs/tests/2015/day19.txt diff --git a/src/holt59/aoc/2015/day19.py b/src/holt59/aoc/2015/day19.py new file mode 100644 index 0000000..db57831 --- /dev/null +++ b/src/holt59/aoc/2015/day19.py @@ -0,0 +1,56 @@ +import sys +from collections import defaultdict + +replacements_s, molecule = sys.stdin.read().split("\n\n") + +REPLACEMENTS: dict[str, list[str]] = defaultdict(list) +for replacement_s in replacements_s.splitlines(): + p = replacement_s.split(" => ") + REPLACEMENTS[p[0]].append(p[1]) +molecule = molecule.strip() + +generated = [ + molecule[:i] + replacement + molecule[i + len(symbol) :] + for symbol, replacements in REPLACEMENTS.items() + for replacement in replacements + for i in range(len(molecule)) + if molecule[i:].startswith(symbol) +] + +answer_1 = len(set(generated)) +print(f"answer 1 is {answer_1}") + +inversion: dict[str, str] = { + replacement: symbol + for symbol, replacements in REPLACEMENTS.items() + for replacement in replacements +} + +# there is actually only one way to create the molecule, and we can greedily replace +# tokens with their replacements, e.g., if H => OH then we can replace OH by H directly +# without thinking + +count = 0 +while molecule != "e": + i = 0 + m2 = "" + while i < len(molecule): + found = False + for replacement in inversion: + if molecule[i:].startswith(replacement): + m2 += inversion[replacement] + i += len(replacement) + count += 1 + found = True + break + + if not found: + m2 += molecule[i] + i += 1 + + # print(m2) + molecule = m2 + + +answer_2 = count +print(f"answer 2 is {count}") diff --git a/src/holt59/aoc/inputs/holt59/2015/day19.txt b/src/holt59/aoc/inputs/holt59/2015/day19.txt new file mode 100644 index 0000000..8bb6fa8 --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day19.txt @@ -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/src/holt59/aoc/inputs/tests/2015/day19.txt b/src/holt59/aoc/inputs/tests/2015/day19.txt new file mode 100644 index 0000000..58e5462 --- /dev/null +++ b/src/holt59/aoc/inputs/tests/2015/day19.txt @@ -0,0 +1,7 @@ +H => HO +H => OH +O => HH +e => H +e => O + +HOH