-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14b.py
22 lines (19 loc) · 842 Bytes
/
14b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from collections import Counter
with open("input.txt") as f:
lines = list(map(lambda x: x.split("\n"), f.read().split("\n\n")))
polymer: str = lines[0][0]
rules: dict[str, str] = dict(rule.split(" -> ") for rule in lines[1])
polymer_counter: dict[tuple[str, str], int] = Counter(zip(polymer, polymer[1:]))
for step in range(40):
step_counter: dict[tuple[str, str], int] = Counter()
for (el_a, el_b), num in polymer_counter.items():
if pol_element := rules.get(el_a + el_b):
step_counter[(el_a, pol_element)] += num
step_counter[(pol_element, el_b)] += num
polymer_counter = step_counter
c = Counter({polymer[-1]: 1})
for (el_a, el_b), num in polymer_counter.items():
c[el_a] += num
c[el_b] += num
most_common = c.most_common()
print(most_common[0][1] // 2 - most_common[-1][1] // 2)