-
Notifications
You must be signed in to change notification settings - Fork 0
/
23.2.py
100 lines (88 loc) · 2.59 KB
/
23.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
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
# cups = [3, 8, 9, 1, 2, 5, 4, 6, 7]
cups = [7, 1, 2, 6, 4, 3, 5, 8, 9]
class Cup(object):
def __init__(self, cup):
self.cup = cup
self.next = None
def __str__(self):
return str(self.cup)
def __repr__(self):
return str(self.cup)
def insert(self, picked_up):
last = self.next
self.next = picked_up
picked_up.next.next.next = last
def pickup(self, target):
current = self
while current.cup != target:
current = current.get_next()
return current
class CupList(object):
def __init__(self, cups):
# generate all cups in ascending order
self.cups = [Cup(cup) for cup in range(1000001)]
# searching was super slow in part 1, use value indexing
# now get our indexes in the order they should be
indexes = cups + list(range(10, 1000001))
# link them all up
for x, y in zip(indexes, indexes[1:]):
self.cups[x].next = self.cups[y]
self.cups[indexes[-1]].next = self.cups[cups[0]]
self.current = self.cups[cups[0]]
def __str__(self):
s = []
cup = self.cups[0]
while cup.next != self.cups[0]:
if cup == self.current:
s.append('({})'.format(cup))
else:
s.append(str(cup))
cup = cup.next
if cup == self.current:
s.append('({})'.format(cup))
else:
s.append(str(cup))
return 'cups: ' + ' '.join(s)
def __repr__(self):
s = []
cup = self.cups[0]
while cup.next != self.cups[0]:
if cup == self.current:
s.append('({})'.format(cup))
else:
s.append(str(cup))
cup = cup.next
if cup == self.current:
s.append('({})'.format(cup))
else:
s.append(str(cup))
return 'cups: ' + ' '.join(s)
def get(self, cup):
return self.cups[cup]
def shuffle(self, turns):
for _ in range(turns):
# print('-- move {} --'.format(_+1))
# print(self)
self.turn()
# print(self)
return self
def turn(self):
p = self.current.next
self.current.next = p.next.next.next
pickedup = [p, p.next, p.next.next]
# print('pick up:', ', '.join([str(c.cup) for c in pickedup]))
if self.current.cup == 1:
destination = len(self.cups) - 1
else:
destination = self.current.cup - 1
while destination in [int(p.cup) for p in pickedup]:
if self.current.cup == 1:
destination = len(self.cups) - 1
else:
destination = self.current.cup - 1
self.get(destination).insert(p)
self.current = self.current.next
cuplist = CupList(cups).shuffle(10000000)
start = cuplist.get(1).next
print(start.cup, start.next.cup)
print(start.cup * start.next.cup)