-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.py
68 lines (65 loc) · 1.85 KB
/
day9.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
import itertools
def part1(players, value):
players = list(range(players))
score = [0]*len(players)
circle = [0, 2, 1]
marble = 3
current = 1
for player in itertools.cycle(players):
if player < 2 and len(circle) == 3:
continue
pos = (current + 2)
if marble % 23 == 0:
score[player] += marble
current -= 7
if current < 0:
current += len(circle)
score[player] += circle.pop(current)
pos = current
elif pos == len(circle):
circle.append(marble)
else:
pos = pos % len(circle)
circle.insert(pos,marble)
current = pos
#print(player, circle)
marble += 1
if marble == value+1:
return max(score)
return max(score)
from collections import deque
def part2(players, value):
players = list(range(players))
score = [0] * len(players)
circle = deque([0])
circle.append(1)
circle.rotate(1)
circle.append(2)
marble = 3
current = 2
for player in itertools.cycle(players):
if player < 2 and len(circle) == 3:
continue
if marble % 23 == 0:
score[player] += marble
circle.rotate(7)
score[player] += circle.pop()
circle.rotate(-1)
else:
circle.rotate(-1)
circle.append(marble)
# print(player, circle)
marble += 1
if marble == value + 1:
return max(score)
return max(score)
def read_data(input):
with open(input, 'r') as fin:
l = next(fin).strip().split()
players, value = int(l[0]), int(l[6])
return players, value
input = 'day9.txt'
#input = 'test.txt'
player, value = read_data(input)
#print('part1:',part1(player, value))
print('part2:',part2(player, value*100))