-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday15_numbers.py
67 lines (54 loc) · 1.61 KB
/
day15_numbers.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
from collections import defaultdict
import pytest
def speak_number(initial, stop_num):
# given history, speak next number
history = defaultdict(list)
turn = 0
for num in initial:
turn += 1
history[num].append(turn)
last_spoken = num
while turn < stop_num:
turn += 1
if len(history[last_spoken]) == 1:
last_spoken = 0
else:
spoken_history = history[last_spoken]
last_spoken = spoken_history[-1] - spoken_history[-2]
history[last_spoken].append(turn)
return last_spoken
@pytest.mark.parametrize(
"list_o_numbers, number",
[
([0, 3, 6], 436),
([1, 3, 2], 1),
([2, 1, 3], 10),
([1, 2, 3], 27),
([2, 3, 1], 78),
([3, 2, 1], 438),
([3, 1, 2], 1836),
],
)
def test_speak_number(list_o_numbers, number):
assert speak_number(list_o_numbers, 2020) == number
@pytest.mark.parametrize(
"list_o_numbers, num_turns",
[
# ([0, 3, 6], 175594),
# ([1, 3, 2], 2578),
# ([2, 1, 3], 3544142),
# ([1, 2, 3], 261214),
# ([2, 3, 1], 6895259),
# ([3, 2, 1], 18),
# ([3, 1, 2], 362),
],
)
def test_speak_number_part_2(list_o_numbers, num_turns):
assert speak_number([0, 3, 6], 30000000) == 436
if __name__ == "__main__":
with open("2020/data/day15_input.txt") as f:
puzzle_input = [int(value) for value in f.read().split(",")]
result = speak_number(puzzle_input, 2020)
print(f"Part 1 is {result}")
result = speak_number(puzzle_input, 30000000)
print(f"Part 2 is {result}")