-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.py
54 lines (45 loc) · 1.59 KB
/
day23.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
from tqdm import tqdm
class Circle:
def __init__(self, cups=None):
self.max = len(cups)
self.circle = {cup: Cup(cup) for cup in cups}
next_cups = cups[1:] + [cups[0]]
for cup, next_cup in zip(cups, next_cups):
self.circle[cup].next = next_cup
self.last = cups[-1]
def move(self, active):
pick_up_1 = self.circle[active].next
pick_up_2 = self.circle[pick_up_1].next
pick_up_3 = self.circle[pick_up_2].next
new_active = self.circle[pick_up_3].next
placement = active
while True:
placement -= 1
if placement == 0:
placement = self.max
if placement not in (pick_up_1, pick_up_2, pick_up_3):
break
prev_next = self.circle[placement].next
self.circle[placement].next = pick_up_1
# self.circle[pick_up_1].next = pick_up_2
# self.circle[pick_up_2].next = pick_up_3
self.circle[pick_up_3].next = prev_next
self.circle[active].next = new_active
return new_active
def end_state(self, length_out=9):
value = 1
values = []
for i in range(length_out):
value = self.circle[value].next
values.append(str(value))
print(' - '.join(values))
class Cup:
def __init__(self, data):
self.data = data
self.next = None
if __name__ == '__main__':
game = Circle([6,5,3,4,2,7,9,1,8] + list(range(10,1000001)))
new_active = 6
for round in tqdm(range(10000000)):
new_active = game.move(new_active)
game.end_state(2)