-
Notifications
You must be signed in to change notification settings - Fork 0
/
14-B.py
47 lines (31 loc) · 972 Bytes
/
14-B.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
from collections import Counter
import itertools
with open("14-input.txt") as file:
lines = [x.strip() for x in file.readlines()]
poly = lines[0]
dic = {}
for line in lines[2:]: # skip empty line and string
k, v = line.split(" -> ")
dic[k] = v
pairs = Counter(map(''.join, itertools.pairwise(poly)))
elements = Counter(poly)
def polymerize(poly, elements):
copy = poly.copy()
for x in copy:
middle = dic[x]
left = x[0] # first char
right = x[1] # second char
num = copy[x]
a = str(left + middle)
b = str(middle+right)
if poly[x] > 0:
poly.update({a: num, b: num})
poly[x] -= num
elements.update({middle: num})
return (poly, elements)
def compute_output(poly):
counter = poly.most_common()
return counter[0][1] - counter[-1][1]
for x in range(40):
pairs, elements = polymerize(pairs, elements)
print(compute_output(elements))