This repository was archived by the owner on Oct 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday09.py
More file actions
executable file
·60 lines (45 loc) · 1.35 KB
/
Copy pathday09.py
File metadata and controls
executable file
·60 lines (45 loc) · 1.35 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from collections import deque
from itertools import cycle
import re
def get_data():
with open('input') as f:
return tuple(map(int, re.findall(r'(\d+)', f.read())))
def part_1(players, last_value):
marbles = [0, 1]
scores = [0] * players
value = 1
curr_idx = 1
for player in cycle(range(players)):
value += 1
if value > last_value:
break
if value % 23 == 0:
curr_idx = (curr_idx - 8 + len(marbles)) % len(marbles) + 1
scores[player] += value + marbles.pop(curr_idx)
else:
curr_idx = (curr_idx + 1) % len(marbles) + 1
marbles.insert(curr_idx, value)
return max(scores)
def part_2(players, last_value):
# part_1 is too slow
# return part_1(players, last_value*100)
marbles = deque([0, 1])
scores = [0] * players
value = 1
for player in cycle(range(players)):
value += 1
if value > last_value:
break
if value % 23 == 0:
marbles.rotate(-7)
scores[player] += value + marbles.pop()
else:
marbles.rotate(2)
marbles.append(value)
return max(scores)
def main():
players, last_value = get_data()
print('1:', part_1(players, last_value))
print('2:', part_2(players, last_value*100))
if __name__ == '__main__':
main()