-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·164 lines (143 loc) · 6.16 KB
/
part_a.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
import re
from collections import namedtuple
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
436720
"""
return MarbleGame.from_marble_game_text(_input).get_high_score()
class MarbleGame(namedtuple("MarbleGame", ("player_count", "marble_count"))):
re_game = re.compile(r"^(\d+) players; last marble is worth (\d+) points$")
@classmethod
def from_marble_game_text(cls, marble_game_text):
"""
>>> MarbleGame.from_marble_game_text(
... "10 players; last marble is worth 1618 points")
MarbleGame(player_count=10, marble_count=1619)
"""
player_count_str, max_marble_value_str = \
cls.re_game.match(marble_game_text).groups()
player_count = int(player_count_str)
marble_count = int(max_marble_value_str) + 1
return cls(player_count, marble_count)
def get_high_score(self):
"""
>>> MarbleGame(10, 1619).get_high_score()
8317
>>> MarbleGame(13, 8000).get_high_score()
146373
>>> MarbleGame(17, 1105).get_high_score()
2764
>>> MarbleGame(21, 6112).get_high_score()
54718
>>> MarbleGame(30, 5808).get_high_score()
37305
"""
scores = [0] * self.player_count
for step, score in self.get_scores():
player = step % self.player_count
scores[player] += score
return max(scores)
def get_scores(self):
for _, _, step, score in self.get_all_steps():
if score:
yield step, score
def play_game(self):
"""
>>> MarbleGame(9, 1).play_game()
((0,), 0, [0, 0, 0, 0, 0, 0, 0, 0, 0])
>>> MarbleGame(9, 26).play_game()
((0, 16, 8, 17, 4, 18, 19, 2, 24, 20, 25, 10, 21, 5, 22, 11, 1, 12, 6, 13, 3, 14, 7, 15), 10, [0, 0, 0, 0, 32, 0, 0, 0, 0])
>>> MarbleGame(9, 47).play_game()
((0, 39, 16, 40, 8, 41, 42, 4, 43, 18, 44, 19, 45, 2, 24, 20, 25, 10, 26, 21, 27, 5, 28, 22, 29, 11, 30, 1, 31, 12, 32, 6, 33, 13, 34, 3, 35, 14, 36, 7, 37, 15, 38), 6, [63, 0, 0, 0, 32, 0, 0, 0, 0])
>>> max(MarbleGame(10, 1619).play_game()[2])
8317
>>> max(MarbleGame(13, 8000).play_game()[2])
146373
>>> max(MarbleGame(17, 1105).play_game()[2])
2764
>>> max(MarbleGame(21, 6112).play_game()[2])
54718
>>> max(MarbleGame(30, 5808).play_game()[2])
37305
"""
scores = [0] * self.player_count
circle = ()
position = 0
for circle, position, step, score in self.get_all_steps():
player = step % self.player_count
scores[player] += score
return circle, position, scores
def get_all_steps(self):
marbles_left = list(range(self.marble_count))
circle = (marbles_left.pop(0),)
position = 0
step = 0
yield circle, position, step, 0
yield from self.exhaust_marbles(circle, position, step, marbles_left)
def exhaust_marbles(self, circle, position, step, marbles_left):
while marbles_left:
marble = marbles_left.pop(0)
circle, position, removed_marbles = \
self.do_step(circle, position, marble)
yield circle, position, step, sum(removed_marbles)
step += 1
def do_step(self, circle, position, marble):
if marble % 23 == 0:
circle, position, removed_marbles =\
self.remove_marbles(circle, position, marble)
else:
circle, position = self.insert_marble(circle, position, marble)
removed_marbles = ()
return circle, position, removed_marbles
def remove_marbles(self, circle, position, marble):
"""
>>> MarbleGame(1, 1).remove_marbles(
... (0, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12,
... 6, 13, 3, 14, 7, 15), 13, 23)
((0, 16, 8, 17, 4, 18, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12, 6, 13, 3, 14, 7, 15), 6, (23, 9))
>>> MarbleGame(1, 1).remove_marbles(
... (0, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12,
... 6, 13, 3, 14, 7, 15), 7, 23)
((16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12, 6, 13, 3, 14, 7, 15), 0, (23, 0))
>>> MarbleGame(1, 1).remove_marbles(
... (0, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12,
... 6, 13, 3, 14, 7, 15), 6, 23)
((0, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12, 6, 13, 3, 14, 7), 0, (23, 15))
>>> MarbleGame(1, 1).remove_marbles(
... (0, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 12,
... 6, 13, 3, 14, 7, 15), 0, 23)
((0, 16, 8, 17, 4, 18, 9, 19, 2, 20, 10, 21, 5, 22, 11, 1, 6, 13, 3, 14, 7, 15), 16, (23, 12))
"""
position = (position - 7 + len(circle)) % len(circle)
removed_marble = circle[position]
circle = circle[:position] + circle[position + 1:]
position = position % len(circle)
return circle, position, (marble, removed_marble)
def insert_marble(self, circle, position, marble):
"""
>>> MarbleGame(1, 1).insert_marble((0,), 0, 1)
((0, 1), 1)
>>> MarbleGame(1, 1).insert_marble((0, 1), 1, 2)
((0, 2, 1), 1)
>>> MarbleGame(1, 1).insert_marble((0, 2, 1), 1, 3)
((0, 2, 1, 3), 3)
>>> MarbleGame(1, 1).insert_marble((0, 2, 1, 3), 3, 4)
((0, 4, 2, 1, 3), 1)
>>> MarbleGame(1, 1).insert_marble((0, 4, 2, 1, 3), 1, 5)
((0, 4, 2, 5, 1, 3), 3)
>>> MarbleGame(1, 1).insert_marble((0, 4, 2, 5, 1, 3), 3, 6)
((0, 4, 2, 5, 1, 6, 3), 5)
>>> MarbleGame(1, 1).insert_marble((0, 4, 2, 5, 1, 6, 3), 5, 7)
((0, 4, 2, 5, 1, 6, 3, 7), 7)
>>> MarbleGame(1, 1).insert_marble((0, 4, 2, 5, 1, 6, 3, 7), 7, 8)
((0, 8, 4, 2, 5, 1, 6, 3, 7), 1)
"""
position = (position + 1) % len(circle) + 1
circle = circle[:position] + (marble,) + circle[position:]
return circle, position
Challenge.main()
challenge = Challenge()