-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.py
61 lines (49 loc) · 1.26 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
NUM_PLAYERS = 458
LAST_MARBLE = 71307
class Player(object):
def __init__(self, player_num):
self.player_num = player_num
self.score = 0
class Marble(object):
def __init__(self, num):
self.num = num
self.next = None
self.prev = None
players = []
for n in xrange(NUM_PLAYERS):
players.append(Player(n))
def insert_fwd(marble, newmarble):
newmarble.next = marble.next
newmarble.prev = marble
marble.next = newmarble
newmarble.next.prev = newmarble
return newmarble
def remove_cur(marble):
marble.prev.next = marble.next
marble.next.prev = marble.prev
new_cur = marble.next
return new_cur
def back_7(marble):
return marble.prev.prev.prev.prev.prev.prev.prev
def play(player, marble, turn):
if turn % 23 == 0:
player.score += turn
marble = back_7(marble)
player.score += marble.num
marble = remove_cur(marble)
else:
marble = insert_fwd(marble.next, Marble(turn))
return marble
cur_marble = Marble(0)
cur_marble.next = cur_marble
cur_marble.prev = cur_marble
turn = 1
playing = True
while playing:
for player in players:
cur_marble = play(player, cur_marble, turn)
turn += 1
if turn >= LAST_MARBLE:
playing = False
break
print "Winning score is %d" % max([x.score for x in players])