-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
92 lines (68 loc) · 2.19 KB
/
day15.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
#!/usr/bin/env python3
import pytest
import time
example1 = [0, 3, 6]
example2 = [1, 3, 2]
example3 = [2, 1, 3]
example4 = [1, 2, 3]
example5 = [2, 3, 1]
example6 = [3, 2, 1]
example7 = [3, 1, 2]
input_list = [0, 8, 15, 2, 12, 1, 4]
@pytest.mark.parametrize("test_input, expected", [
(example1, 436),
(example2, 1),
(example3, 10),
(example4, 27),
(example5, 78),
(example6, 438),
(example7, 1836)
])
def test_part_one(test_input, expected):
assert part_one(test_input) == expected
def part_one(input_list):
x = play_game(input_list, 2020)
print("Part1: ", x)
return x
@pytest.mark.parametrize("test_input, expected", [
(example1, 175594),
(example2, 2578),
(example3, 3544142),
(example4, 261214),
(example5, 6895259),
(example6, 18),
(example7, 362)
])
def test_part_two(test_input, expected):
assert part_two(test_input) == expected
def part_two(input_list):
x = play_game(input_list, 30000000)
print("Part2: ", x)
return x
def play_game(input_list, max_index):
max_index = max_index
indexes = {x: "X" for x in range(0, max_index)}
# last item will be processed below
for idx, x in enumerate(input_list[:-1]):
indexes[x] = idx + 1
# e.g. if input_list has 3 items, we are starting turn 4
turn = len(input_list) + 1
last_added = input_list[-1]
while turn <= max_index:
if indexes[last_added] == "X":
next_number = 0
else:
next_number = turn - 1 - indexes[last_added]
indexes[last_added] = turn - 1
last_added = next_number
turn += 1
return last_added
if __name__ == "__main__":
start = time.time()
part_one(input_list)
stop = time.time()
print("time: ", stop - start)
start = time.time()
part_two(input_list)
stop = time.time()
print("time: ", stop - start)