-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
53 lines (35 loc) · 1.27 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
48
49
50
51
52
53
#!/usr/bin/env python3
from collections import Counter
def parse_input():
with open("./input") as f:
template, rules_raw = f.read().split("\n\n")
rules = {}
for rule in rules_raw.split("\n"):
l, r = rule.split("->")
rules[l.strip()] = r.strip()
return template, rules
def step(rules, char_counter, pair_counter):
for p, c in Counter(pair_counter).items():
if c > 0 and p in rules:
lp = p[0] + rules[p]
rp = rules[p] + p[1]
pair_counter[p] -= c
pair_counter[lp] += c
pair_counter[rp] += c
char_counter[rules[p]] += c
return char_counter, pair_counter
def solve(template, rules, rounds):
char_counter = Counter(template)
pair_counter = Counter([template[i:i+2]
for i in range(len(template) + 1)])
for _ in range(rounds):
char_counter, pair_counter = step(rules, char_counter, pair_counter)
mc = char_counter.most_common(1)
lc = char_counter.most_common()[:-2:-1]
return mc[0][1] - lc[0][1]
def main():
template, rules = parse_input()
print(f"part1: {solve(template, rules, 10)}")
print(f"part2: {solve(template, rules, 40)}")
if __name__ == "__main__":
main()