-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Day19 solution. Used regexes and slicing, even though there were …
…probably better solutions.
- Loading branch information
Zachary Boerner
authored and
Zachary Boerner
committed
Jan 1, 2016
1 parent
e533330
commit b5ee1f5
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |