-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
47 lines (37 loc) · 1.28 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
47
from collections import defaultdict
from itertools import pairwise
from operator import itemgetter
from aoc import get_input
polymer, rule_lines = get_input(day=14, as_list=False).split('\n\n')
pairs = defaultdict(int)
for a, b in pairwise(polymer):
pairs[a+b] += 1
rules = {}
for rule in rule_lines.splitlines():
key, value = rule.split(' -> ')
rules[key] = value
def count_single_letters(pairs):
counts = defaultdict(int)
for pair, count in pairs.items():
a, b = pair
counts[a] += count / 2
counts[b] += count / 2
counts[polymer[0]] += 0.5
counts[polymer[-1]] += 0.5
return {k: int(v) for k, v in counts.items()}
for t in range(1, 40+1):
new_pairs = defaultdict(int)
for pair, count in pairs.items():
if pair in rules:
a, b = pair
inserted = rules[pair]
new_pairs[a+inserted] += count
new_pairs[inserted+b] += count
else:
new_pairs[pair] += count
pairs = new_pairs
if t == 10 or t == 40:
letter_counts = [v for k, v in sorted(count_single_letters(pairs).items(),
key=itemgetter(1),
reverse=True)]
print(letter_counts[0] - letter_counts[-1])