-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14b.py
40 lines (29 loc) · 1.01 KB
/
day14b.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
from collections import defaultdict
def compute(level, a, b, cache, patterns):
pattern_key = a + b
key = pattern_key + str(level)
if level == 0:
counts = { a: 1 }
cache[key] = counts
return counts
if key in cache:
return cache[key]
sub = patterns[pattern_key]
counts = defaultdict(lambda: 0)
for k, v in compute(level - 1, a, sub, cache, patterns).items():
counts[k] += v
for k, v in compute(level - 1, sub, b, cache, patterns).items():
counts[k] += v
cache[key] = counts
return counts
with open("day14.txt") as file:
seed = list(file.readline().strip())
file.readline()
patterns = dict(line.strip().split(' -> ') for line in file.readlines())
cache = {}
counts = defaultdict(lambda: 0)
for pos in range(0, len(seed) - 1):
for k, v in compute(40, seed[pos], seed[pos+1], cache, patterns).items():
counts[k] += v
counts[seed[-1]] += 1
print max(counts.values()) - min(counts.values())