-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
235 lines (190 loc) · 5.49 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from collections import Counter, defaultdict
import unittest
from input import read_and_solve
def parse_input(lines):
template_line, rule_lines = (lines[0], lines[2:])
template = list(template_line)
rule_pairs = [list(rule.split(" -> ")) for rule in rule_lines]
rules = {key: value for key, value in rule_pairs}
return template, rules
def parse_input_2(lines):
template_line, rule_lines = (lines[0], lines[2:])
rule_pairs = [list(rule.split(" -> ")) for rule in rule_lines]
rules = {key: value for key, value in rule_pairs}
chars = list(template_line)
pairs = defaultdict(int)
for i in range(len(chars) - 1):
key = chars[i] + chars[i+1]
pairs[key] += 1
return pairs, rules
def step(template, rules):
new_template = []
for i in range(len(template) - 1):
c1, c2 = template[i], template[i+1]
to_insert = rules[c1 + c2]
new_template.append(c1)
new_template.append(to_insert)
new_template.append(template[-1])
return new_template
def step_2(pairs, char_counter, rules):
new_pairs = defaultdict(int)
for pair, count in pairs.items():
middle = rules[pair]
seq1 = pair[0] + middle
seq2 = middle + pair[1]
new_pairs[seq1] += count
new_pairs[seq2] += count
char_counter[middle] += count
return new_pairs
def find_extremes(template):
histogram = Counter(template)
counts = histogram.values()
return min(counts), max(counts)
def find_extremes_2(char_counter):
counts = char_counter.values()
return min(counts), max(counts)
def part1(lines):
template, rules = parse_input(lines)
for _ in range(10):
template = step(template, rules)
min, max = find_extremes(template)
return max - min
def part2(lines):
pairs, rules = parse_input_2(lines)
char_counter = Counter(lines[0])
for _ in range(40):
pairs = step_2(pairs, char_counter, rules)
min, max = find_extremes_2(char_counter)
return max - min
class TestDay14(unittest.TestCase):
sample = [
"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",
]
def test_parse_input(self):
template, rules = parse_input(self.sample)
self.assertEqual(template, ['N', 'N', 'C', 'B'])
self.assertEqual(rules, {
"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",
})
def test_parse_input_2(self):
pairs, rules = parse_input_2(self.sample)
self.assertEqual(pairs, { # ['N', 'N', 'C', 'B'])
"CB": 1,
"NN": 1,
"NC": 1,
})
self.assertEqual(rules, {
"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",
})
def test_step(self):
result = step(list("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",
})
self.assertEqual(result, list("NCNBCHB"))
def test_step_2(self):
pairs = defaultdict(int)
pairs["CB"] = 1
pairs["NN"] = 1
pairs["NC"] = 1
char_counter = Counter("NNCB")
new_pairs = step_2(pairs, char_counter, {
"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",
})
self.assertEqual(new_pairs["BC"], 1)
self.assertEqual(new_pairs["CH"], 1)
self.assertEqual(new_pairs["CN"], 1)
self.assertEqual(new_pairs["HB"], 1)
self.assertEqual(new_pairs["NB"], 1)
self.assertEqual(new_pairs["NC"], 1)
self.assertEqual(char_counter["N"], 2)
self.assertEqual(char_counter["C"], 2)
self.assertEqual(char_counter["B"], 2)
self.assertEqual(char_counter["H"], 1)
def test_find_extremes(self):
min, max = find_extremes("AAACAAABBDDDAAA")
self.assertEqual(min, 1)
self.assertEqual(max, 3 * 3)
def test_part_1_sample(self):
self.assertEqual(part1(self.sample), 1588)
def test_part_2_sample(self):
self.assertEqual(part2(self.sample), 2188189693529)
if __name__ == '__main__':
unittest.main(exit=False)
read_and_solve(__file__, part1, part2)