-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15.py
More file actions
36 lines (31 loc) · 1.09 KB
/
15.py
File metadata and controls
36 lines (31 loc) · 1.09 KB
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
from collections import defaultdict
with open("input15.txt") as f:
lines = [x.strip() for x in f.readlines()]
class Game:
def __init__(self, starting_numbers):
self.last = defaultdict(lambda: None)
self.before_last = defaultdict(lambda: None)
self.last_num = 0
self.times = 1
for i, n in enumerate(starting_numbers):
self.last[n] = i + 1
self.last_num = n
self.times += 1
def do_game(self):
if not self.before_last[self.last_num]:
self.before_last[0] = self.last[0]
self.last[0] = self.times
self.last_num = 0
else:
prev = self.last[self.last_num] - self.before_last[self.last_num]
self.before_last[prev] = self.last[prev]
self.last[prev] = self.times
self.last_num = prev
self.times += 1
for l in lines:
starting_numbers = [int(x) for x in l.split(',')]
g = Game(starting_numbers)
rounds = 30000000
for times in range(len(starting_numbers), rounds):
g.do_game()
print(g.last_num)