2015 day 19.
This commit is contained in:
parent
2d23e355b2
commit
cfa7718475
56
src/holt59/aoc/2015/day19.py
Normal file
56
src/holt59/aoc/2015/day19.py
Normal file
@ -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}")
|
45
src/holt59/aoc/inputs/holt59/2015/day19.txt
Normal file
45
src/holt59/aoc/inputs/holt59/2015/day19.txt
Normal file
@ -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
|
7
src/holt59/aoc/inputs/tests/2015/day19.txt
Normal file
7
src/holt59/aoc/inputs/tests/2015/day19.txt
Normal file
@ -0,0 +1,7 @@
|
||||
H => HO
|
||||
H => OH
|
||||
O => HH
|
||||
e => H
|
||||
e => O
|
||||
|
||||
HOH
|
Loading…
Reference in New Issue
Block a user