-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay19.py
54 lines (40 loc) · 1.38 KB
/
Day19.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
with open("Day19inputB.txt") as f:
data = f.readlines()
replacements = []
for line in data[:-2]:
words = line.split()
replacements.append([words[0], words[2]])
molecule = data[-1]
products = set()
for rule in replacements:
start = 0
place = molecule.find(rule[0], start)
while place >= 0:
first_part = molecule[:place]
second_part = molecule[place:]
new_molecule = first_part + second_part.replace(rule[0], rule[1], 1)
products.add(new_molecule)
start = place + len(rule[0])
place = molecule.find(rule[0], start)
print(len(products))
# part 2
def try_shorten(replacements, shorties_by_steps, steps):
while True:
mol = shorties_by_steps[steps-1]
rule_string = ""
for rule in replacements:
place = mol.find(rule[1])
if place == -1:
continue
new_mol = mol.replace(rule[1], rule[0], 1)
if new_mol == "e":
return steps
else:
shorties_by_steps.append(new_mol)
rule_string = rule[1] + " => " + rule[0]
break
# print("step {}: {} {} [length:{}]".format(steps, rule_string, shorties_by_steps[steps], len(shorties_by_steps[steps])))
steps += 1
shorties_by_steps = [molecule]
min_steps = try_shorten(replacements, shorties_by_steps, 1)
print(min_steps)