-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc9.2.py
executable file
·72 lines (53 loc) · 1.6 KB
/
aoc9.2.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
#!/usr/bin/env python3
import sys
class Marble:
def __init__(self, value):
self.value = value
self.cw = None
self.ccw = None
def place(self, other):
'''Inserts a new marble in the circle according to the stupid elven
rules. Returns the new current marble.
'''
before = self.cw
after = self.cw.cw
# print("before {}, after {}".format(before.value, after.value))
other.ccw = before
other.cw = after
before.cw = other
after.ccw = other
return other
def remove(self):
'''Remove the marble 7 marbles ccw from this. Returns tuple of
(new current marble, value of removed)
'''
removed = self
for _ in range(7):
removed = removed.ccw
before = removed.ccw
after = removed.cw
before.cw = after
after.ccw = before
return (after, removed.value)
def main():
try:
num_players = int(sys.argv[1])
num_marbles = int(sys.argv[2])
except (IndexError, ValueError):
print("Usage: {} NUM_PLAYERS NUM_MARBLES".format(sys.argv[0]), file=sys.stderr)
return
scores = [0] * num_players
m = Marble(0)
m.cw = m
m.ccw = m
player = 0
for value in range(1, num_marbles + 1):
if value % 23 != 0:
m = m.place(Marble(value))
else:
(m, removed) = m.remove()
scores[player] += value + removed
player = (player + 1) % num_players
print("High score: {}".format(max(scores)))
if __name__ == '__main__':
main()