-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.py
More file actions
84 lines (62 loc) · 1.6 KB
/
day14.py
File metadata and controls
84 lines (62 loc) · 1.6 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from collections import Counter
import parse
from run_util import run_puzzle
def parse_data(data):
template, mapping_strings = data.split('\n\n')
pairs = Counter(zip(template, template[1:]))
mappings = {
(a, b): [(a, c), (c, b)]
for a, b, c in parse.findall('{:l}{:l} -> {:l}', mapping_strings)
}
return pairs, mappings, template
def do_step(pairs, mappings):
new_pairs = Counter()
for source in pairs.keys():
for destination in mappings[source]:
new_pairs[destination] += pairs[source]
return new_pairs
def get_answer(pairs, template):
counter = Counter()
for pair, count in pairs.items():
counter[pair[0]] += count
counter[pair[1]] += count
counter[template[0]] += 1
counter[template[-1]] += 1
frequency = counter.most_common()
most = frequency[0][1]
least = frequency[-1][1]
return (most - least) // 2
def part_a(data):
pairs, mappings, template = parse_data(data)
for step in range(10):
pairs = do_step(pairs, mappings)
return get_answer(pairs, template)
def part_b(data):
pairs, mappings, template = parse_data(data)
for step in range(40):
pairs = do_step(pairs, mappings)
return get_answer(pairs, template)
def main():
examples = [
("""NNCB
CH -> B
HH -> N
CB -> H
NH -> C
HB -> C
HC -> B
HN -> C
NN -> C
BH -> H
NC -> B
NB -> B
BN -> B
BB -> N
BC -> B
CC -> N
CN -> C""", 1588, 2188189693529)
]
day = int(__file__.split('/')[-1].split('.')[0][-2:])
run_puzzle(day, part_a, part_b, examples)
if __name__ == '__main__':
main()