mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Solutions to day 19, finally.
This commit is contained in:
45
day-19/input.txt
Normal file
45
day-19/input.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
|
||||
|
||||
CRnSiRnCaPTiMgYCaPTiRnFArSiThFArCaSiThSiThPBCaCaSiRnSiRnTiTiMgArPBCaPMgYPTiRnFArFArCaSiRnBPMgArPRnCaPTiRnFArCaSiThCaCaFArPBCaCaPTiTiRnFArCaSiRnSiAlYSiThRnFArArCaSiRnBFArCaCaSiRnSiThCaCaCaFYCaPTiBCaSiThCaSiThPMgArSiRnCaPBFYCaCaFArCaCaCaCaSiThCaSiRnPRnFArPBSiThPRnFArSiRnMgArCaFYFArCaSiRnSiAlArTiTiTiTiTiTiTiRnPMgArPTiTiTiBSiRnSiAlArTiTiRnPMgArCaFYBPBPTiRnSiRnMgArSiThCaFArCaSiThFArPRnFArCaSiRnTiBSiThSiRnSiAlYCaFArPRnFArSiThCaFArCaCaSiThCaCaCaSiRnPRnCaFArFYPMgArCaPBCaPBSiRnFYPBCaFArCaSiAl
|
||||
57
day-19/solution.py
Normal file
57
day-19/solution.py
Normal file
@@ -0,0 +1,57 @@
|
||||
from __future__ import print_function
|
||||
import fileinput
|
||||
import re
|
||||
|
||||
def doReplace(replacement, medicine):
|
||||
replacements = []
|
||||
for match in re.finditer(replacement[0], medicine):
|
||||
pos = match.start()
|
||||
sub = medicine[:pos] + replacement[1] + medicine[pos + len(replacement[0]):]
|
||||
replacements.append(sub)
|
||||
|
||||
return replacements
|
||||
|
||||
def allReplacements(replacements, medicine):
|
||||
options = set()
|
||||
# Simply attempt all replacements
|
||||
for replacement in replacements:
|
||||
for option in doReplace(replacement, medicine):
|
||||
options.add(option)
|
||||
|
||||
return options
|
||||
|
||||
|
||||
# Exhaustive search trying to work back from the medicine.
|
||||
#
|
||||
# This is minimal because we try the larger substitutions first.
|
||||
def solve(replacements, target, current):
|
||||
if current == target:
|
||||
return 0
|
||||
|
||||
for org, rep in replacements:
|
||||
for option in doReplace((rep, org), current):
|
||||
result = solve(replacements, target, option)
|
||||
if result is not None:
|
||||
return result + 1
|
||||
|
||||
return None
|
||||
|
||||
replacements = []
|
||||
medicine = None
|
||||
|
||||
for line in fileinput.input():
|
||||
if len(line.strip()) == 0:
|
||||
continue
|
||||
|
||||
match = re.match(r"(\w+) => (\w+)", line)
|
||||
if match:
|
||||
replacements.append(match.group(1, 2))
|
||||
else:
|
||||
medicine = line.strip()
|
||||
|
||||
replacements.sort(key=lambda x: -len(x[1]))
|
||||
|
||||
|
||||
print("Options:", len(allReplacements(replacements, medicine)))
|
||||
|
||||
print("Steps:", solve(replacements, 'e', medicine))
|
||||
Reference in New Issue
Block a user