Skip to content

Commit

Permalink
Add Day19 solution. Used regexes and slicing, even though there were …
Browse files Browse the repository at this point in the history
…probably better solutions.
  • Loading branch information
Zachary Boerner authored and Zachary Boerner committed Jan 1, 2016
1 parent e533330 commit b5ee1f5
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Day19/Day19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from collections import defaultdict
import re

replacements = defaultdict(list)
molecule = ''
finalMolecules = []

with open('Day19Input.txt', 'r') as file:
for line in file:
line = line.strip()

if '=>' not in line:
print('Final Molecule: \'%s\'' % line)
molecule = line
else:
molecules = line.split(' => ')
replacements[molecules[0]].append(molecules[1])
print('Replace %s with %s' % (molecules[0], molecules[1]))

for key in replacements:
print("Finding: %s" % key)
for replacement in replacements[key]:
for match in re.finditer(key, molecule):
#print(molecule.replace(match, replacements[key]))
print(match)
newMolecule = molecule[0:match.start()]
newMolecule += replacement
newMolecule += molecule[match.end():]
print("New molecule: %s" % newMolecule)
# Look https://docs.python.org/3/library/re.html#match-objects for stuff on the match object returned
finalMolecules.append(newMolecule)

print(len(set(finalMolecules)))
44 changes: 44 additions & 0 deletions Day19/Day19Input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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

0 comments on commit b5ee1f5

Please sign in to comment.