-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2020_15.py
71 lines (54 loc) · 1.82 KB
/
AoC2020_15.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
#! /usr/bin/env python3
#
# Advent of Code 2020 Day 15
#
from aoc import my_aocd
from aoc.common import log
def _parse(inputs: tuple[str]) -> tuple[int]:
assert len(inputs) == 1
return tuple([int(n) for n in inputs[0].split(",")])
def _play(starting_numbers: tuple[int], number_of_turns: int) -> int:
log(f"{starting_numbers} ({number_of_turns} turns)")
turns = [-1] * number_of_turns
for i, n in enumerate(starting_numbers[:-1], start=1):
turns[n] = i
prev = starting_numbers[-1]
for i in range(len(starting_numbers), number_of_turns):
prev_prev = turns[prev]
turns[prev] = i
prev = 0 if prev_prev == -1 else i - prev_prev
return prev
def part_1(inputs: tuple[str]) -> int:
return _play(starting_numbers=_parse(inputs), number_of_turns=2020)
def part_2(inputs: tuple[str]) -> int:
return _play(starting_numbers=_parse(inputs), number_of_turns=30000000)
TEST1 = "0,3,6".splitlines()
TEST2 = "1,3,2".splitlines()
TEST3 = "2,1,3".splitlines()
TEST4 = "1,2,3".splitlines()
TEST5 = "2,3,1".splitlines()
TEST6 = "3,2,1".splitlines()
TEST7 = "3,1,2".splitlines()
def main() -> None:
my_aocd.print_header(2020, 15)
assert part_1(TEST1) == 436
assert part_1(TEST2) == 1
assert part_1(TEST3) == 10
assert part_1(TEST4) == 27
assert part_1(TEST5) == 78
assert part_1(TEST6) == 438
assert part_1(TEST7) == 1836
assert part_2(TEST1) == 175594
assert part_2(TEST2) == 2578
assert part_2(TEST3) == 3544142
assert part_2(TEST4) == 261214
assert part_2(TEST5) == 6895259
assert part_2(TEST6) == 18
assert part_2(TEST7) == 362
inputs = my_aocd.get_input(2020, 15, 1)
result1 = part_1(inputs)
print(f"Part 1: {result1}")
result2 = part_2(inputs)
print(f"Part 2: {result2}")
if __name__ == '__main__':
main()