-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
46 lines (37 loc) · 1.39 KB
/
day14.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
import sys
from collections import Counter
def grow(template, formula, steps=10):
characterCounter = Counter(template)
pairs = [ a+b for a,b in zip(template, template[1:])]
pairCounter = Counter(pairs)
for step in range(steps):
stepCounter = Counter()
for pair, count in pairCounter.items():
# Count the character being inserted
middleChar = formula[pair]
characterCounter[middleChar] += count
# Determine newly created pairs and count them, too
leftPair = pair[0] + middleChar
rightPair = middleChar + pair[1]
stepCounter[leftPair] += count
stepCounter[rightPair] += count
pairCounter = stepCounter
return characterCounter
def main(args = ()):
fileName = "day14.txt" if len(args) < 1 else args[0]
formula = {}
template = ""
with open(fileName) as lines:
for line in lines:
line = line.strip()
if line and "->" not in line:
template = line
elif "->" in line:
pair, insert = line.split(" -> ")
formula[pair] = insert
for part, steps in enumerate((10, 40)):
counter = grow(template, formula, steps)
mc = counter.most_common()
print(f"Part {part+1}:", mc[0][1] - mc[-1][1])
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))