-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.py
executable file
·45 lines (35 loc) · 1023 Bytes
/
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
#!/usr/bin/env python3
# [Day 15: Rambunctious Recitation](https://adventofcode.com/2020/day/15)
import sys
from pathlib import Path
verbose = "-v" in sys.argv
if verbose:
sys.argv.remove("-v")
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
nums = list(map(int, data.split(",")))
last_spoken = {}
last_last_spoken = {}
for turn, n in enumerate(nums, 1):
first_spoken = n not in last_spoken
if n in last_spoken:
last_last_spoken[n] = last_spoken[n]
last_spoken[n] = turn
last = n
turn = len(nums) + 1
while True:
if first_spoken:
n = 0
else:
n = last_spoken[last] - last_last_spoken[n]
if turn == 2020:
print(n) # part 1
if turn == 30_000_000:
print(n) # part 2
break
first_spoken = n not in last_spoken
if n in last_spoken:
last_last_spoken[n] = last_spoken[n]
last_spoken[n] = turn
turn += 1
last = n