-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14-1.py
executable file
·48 lines (34 loc) · 1.13 KB
/
14-1.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
#!/usr/bin/env python
from collections import deque
import re
class Polymer:
def __init__(self, template):
self.molecule = template
self.rules = {}
def add_rule(self, pair, insert):
self.rules[pair] = insert
def step(self):
new_molecule = self.molecule[0]
for pos in range(len(self.molecule) - 1):
pair = self.molecule[pos : pos + 2]
new_molecule += self.rules[pair] + pair[1]
self.molecule = new_molecule
def score(self):
counts = {}
for atom in self.molecule:
counts[atom] = counts.get(atom, 0) + 1
l = list(counts.values())
l.sort()
return l[-1] - l[0]
def __str__(self):
return f"<{self.__class__.__name__}: {self.molecule}>"
insertion_format = re.compile("(?P<pair>[A-Z]{2}) -> (?P<insert>[A-Z])")
input = deque([line.strip() for line in open('14.input').readlines()])
p = Polymer(input.popleft())
input.popleft()
for line in input:
insertion = insertion_format.match(line)
p.add_rule(insertion['pair'], insertion['insert'])
for _ in range(10):
p.step()
print(p.score())