-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.py
executable file
·219 lines (196 loc) · 3.52 KB
/
14.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
#!/usr/bin/env python3
# INPUT
import re
from collections import Counter
from functools import cache
def parser(text) -> tuple:
template, rules = text.strip().split('\n\n')
return template, {k: v for k, v in sorted(re.findall(r'(.*) -> (.*)', rules))}
def read_input() -> str:
with open(__file__, encoding="utf-8") as f:
c = f.read()
data = c[c.rindex("🎅") + 1: c.rindex("⛄")].strip()
return data
# MAIN
# brute-force
def run_bf(template: str, rules: dict) -> str:
res = []
for i in range(1, len(template)):
window = template[i - 1:i + 1]
res.append(template[i - 1])
res.append(rules[window])
res.append(template[-1])
return ''.join(res)
class FrozenDict(dict):
def __hash__(self):
return hash(frozenset(self.items()))
@cache
def run(template: str, rules: FrozenDict, step: int, root=True) -> FrozenDict:
if step == 0:
return FrozenDict(Counter(template[:-1]))
res = Counter()
for i in range(1, len(template)):
window = template[i - 1:i + 1]
insert = rules[window]
new_inner = template[i - 1] + insert + template[i]
inner_res = run(new_inner, rules, step - 1, False)
res += Counter(inner_res)
if root:
res += Counter(template[-1])
return FrozenDict(res)
def part1(template: str, rules: dict, steps=10) -> int:
return part2(template, rules, steps)
def part2(template: str, rules: dict, steps=40) -> int:
counter = run(template, FrozenDict(rules), steps)
char_count = counter.values()
return max(char_count) - min(char_count)
# TEST
def test():
# GIVEN
sample_input = """
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
"""
given = parser(sample_input)
assert run_bf('NNCB', given[1]) == 'NCNBCHB'
assert run_bf('NCNBCHB', given[1]) == 'NBCCNBBBCBHCB'
assert run_bf('NBCCNBBBCBHCB', given[1]) == 'NBBBCNCCNBBNBNBBCHBHHBCHB'
assert run_bf('NBBBCNCCNBBNBNBBCHBHHBCHB', given[1]) == 'NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB'
assert part1(*given) == 1588
# part 2
assert run_bf(run_bf('NN', given[1]), given[1]) == 'NBCCN'
rules_t = FrozenDict(given[1])
assert run('NN', rules_t, 2) == Counter('NBCCN')
assert run('NNCB', rules_t, 4) == Counter('NBBNBNBBCCNBCNCCNBBNBBNBBBNBBNBBCBHCBHHNHCBBCBHCB')
assert part2(*given) == 2188189693529
return True
if __name__ == '__main__':
assert test()
input_ = parser(read_input())
# ONE #1
part_1 = part1(*input_)
print(part_1)
assert part_1 == 3048
# TWO #2
part_2 = part2(*input_)
print(part_2)
assert part_2 == 3_288_891_573_057
# INPUT
"""🎅
PKHOVVOSCNVHHCVVCBOH
NO -> B
PV -> P
OC -> K
SC -> K
FK -> P
PO -> P
FC -> V
KN -> V
CN -> O
CB -> K
NF -> K
CO -> F
SK -> F
VO -> B
SF -> F
PB -> F
FF -> C
HC -> P
PF -> B
OP -> B
OO -> V
OK -> N
KB -> H
PN -> V
PP -> N
FV -> S
BO -> O
HN -> C
FP -> F
BP -> B
HB -> N
VC -> F
PC -> V
FO -> O
OH -> S
FH -> B
HK -> B
BC -> F
ON -> K
FN -> N
NN -> O
PH -> P
KS -> H
HV -> F
BK -> O
NP -> S
CC -> H
KV -> V
NB -> C
NS -> S
KO -> V
NK -> H
HO -> C
KC -> P
VH -> C
VK -> O
CP -> K
BS -> N
BB -> F
VV -> K
SH -> O
SO -> N
VF -> K
NV -> K
SV -> O
NH -> C
VS -> N
OF -> N
SP -> C
HP -> O
NC -> V
KP -> B
KH -> O
SN -> S
CS -> N
FB -> P
OB -> H
VP -> B
CH -> O
BF -> B
PK -> S
CF -> V
CV -> S
VB -> P
CK -> H
PS -> N
SS -> C
OS -> P
OV -> F
VN -> V
BV -> V
HF -> B
FS -> O
BN -> K
SB -> N
HH -> S
BH -> S
KK -> H
HS -> K
KF -> V
⛄"""