-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.py
executable file
·353 lines (314 loc) · 5.6 KB
/
10.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
# https://alexgolec.dev/google-interview-questions-deconstructed-the-knights-dialer/
# HELPER FUNCTIONS
def parser(text) -> list:
return [int(l) for l in text.strip().split("\n")]
def read_data() -> list:
with open(__file__, encoding="utf-8") as f:
c = f.read()
data = c[c.rindex("🎅") + 1: c.rindex("⛄")].rstrip()
return parser(data)
# MAIN FUNCTIONS
def find_differences(nums: list) -> dict:
jolt, _nums, diffs = 0, sorted(nums), {1: 0, 2: 0, 3: 0}
for adapter in sorted(_nums):
diff = adapter - jolt
if 0 < diff < 4:
jolt = adapter
diffs[diff] += 1
else:
break
# Finally, your device's built-in adapter is always 3 higher than the highest adapter
diffs[3] += 1
diffs['adapter'] = jolt + 3
return diffs
def find_joltage(diffs: dict) -> int:
return diffs[1] * diffs[3]
# PART 2
# approach 1 - produce arrangements and count the length
# too much space and time
def produce_sequence(arrangements, start: int, opts: list):
"""
NB: doesn't work for big input.
Actually producing sequences, generating sequences for input takes too much time
"""
new_sequences = []
for l in arrangements:
if start < l[-1]:
continue
l.append(opts[0])
i, n = 1, len(opts)
while i < n:
new_seq = l[0:-1]
new_seq.append(opts[i])
new_sequences.append(new_seq)
i += 1
arrangements += new_sequences
def find_arrangements1(nums: list) -> int:
adapter_map = to_adapter_map(nums)
arrangements = [[0]]
for k, v in adapter_map.items():
produce_sequence(arrangements, k, v)
return len(arrangements)
# approach 2 - keep track of leaves and the count the length of leaves
def count_sequences_by_keeping_track_of_leaves(leaves, start: int, opts: list):
"""
keep track of leaves in sequence tree
"""
i, n = 0, len(leaves)
while i < n:
if start == leaves[i]:
leaves[i:i + 1] = opts
i += len(opts)
n += len(opts) - 1
else:
i += 1
def find_arrangements2(nums: list) -> int:
adapter_map = to_adapter_map(nums)
leaves = [0]
for k, v in adapter_map.items():
count_sequences_by_keeping_track_of_leaves(leaves, k, v)
return len(leaves)
def to_adapter_map(nums):
jolt, i, n, _nums = 0, 0, len(nums), sorted(nums)
# optional = set()
adapter_map = {}
while i < n:
adapter = _nums[i]
if 0 < adapter - jolt < 4:
if jolt in adapter_map:
adapter_map[jolt].append(adapter)
else:
adapter_map[jolt] = [adapter]
j = i + 1
while j < n and 0 < _nums[j] - jolt < 4:
# optional.add(_nums[j - 1])
adapter_map[jolt].append(_nums[j])
j += 1
jolt = adapter
else:
break
i += 1
return adapter_map
# approach 3 - FINAL one
def count_sequences(leaves: dict, start: int, opts: list):
"""
keep track of leaves in sequence tree
"""
if start in leaves:
curr = leaves[start]
del leaves[start]
for o in opts:
leaves[o] = (leaves[o] if o in leaves else 0) + curr
def find_arrangements(nums: list) -> int:
adapter_map = to_adapter_map(nums)
leaves = {0: 1}
for k, v in adapter_map.items():
count_sequences(leaves, k, v)
# all sequences must end with the same node
assert len(leaves.keys()) == 1
return leaves.popitem()[1]
# borrowed from https://github.com/sophiebits/adventofcode/blob/main/2020/day10.py#L41
def countways2(nums):
top = max(nums) + 3
nums = set(nums)
nums.add(top)
a, b, c = 0, 0, 1
for i in range(1, top + 1):
if i in nums:
a, b, c = b, c, a + b + c
else:
a, b, c = b, c, 0
return c
def countways(memo, adapters, start, goal):
k = (len(adapters), start)
if k in memo:
return memo[k]
ways = 0
if goal - start <= 3:
ways += 1
if not adapters:
return ways
if adapters[0] - start <= 3:
ways += countways(memo, adapters[1:], adapters[0], goal)
ways += countways(memo, adapters[1:], start, goal)
memo[k] = ways
return ways
# TEST
def test():
# GIVEN
given = """
16
10
15
5
1
11
7
19
6
12
4
"""
given2 = """
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3
"""
assert {1: 7, 2: 0, 3: 5, 'adapter': 22} == find_differences(parser(given))
assert {1: 22, 2: 0, 3: 10, 'adapter': 52} == find_differences(parser(given2))
assert 8 == find_arrangements(parser(given))
assert 19208 == find_arrangements(parser(given2))
# assert 19208 == find_arrangements1(parser(given2))
# assert 19208 == find_arrangements2(parser(given2))
if __name__ == '__main__':
test()
lines = read_data()
# ONE
part_1 = find_joltage(find_differences(lines))
print(part_1)
assert part_1 == 2475
# TWO
# part_2 = countways({}, sorted(lines), 0, max(lines) + 3)
# part_2 = countways2(lines)
part_2 = find_arrangements(lines)
print(part_2)
assert part_2 == 442136281481216
# INPUT
"""🎅
48
171
156
51
26
6
80
62
65
82
130
97
49
31
142
83
75
20
154
119
56
114
92
33
140
74
118
1
96
44
128
134
121
64
158
27
17
101
59
12
89
88
145
167
11
3
39
43
105
16
170
63
111
2
108
21
146
77
45
52
32
127
147
76
58
37
86
129
57
133
120
163
138
161
139
71
9
141
168
164
124
157
95
25
38
69
87
155
135
15
102
70
34
42
24
50
68
169
10
55
117
30
81
151
100
162
148
⛄"""