-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol23.py
204 lines (143 loc) · 5.25 KB
/
sol23.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import utils
from llist import dllist, dllistnode
def initialise():
global cups, currentCupIdx, totalCups, move
cups = utils.loadInputFile("input_23.txt")
cups = [int(cup) for cup in cups[0]]
currentCupIdx = 0
totalCups = len(cups)
move = 1
def printCups():
global cups, currentCup, currentCupIdx
print("cups:", end=" ")
for cup in cups:
if cup is currentCup:
print("(", cup, ")", sep="", end=" ")
else:
print(cup, end=" ")
print()
def safeWrap(n):
global totalCups
if n >= totalCups:
return n - totalCups
if n < 0:
return n + totalCups
else:
return n
def processMove():
global cups, currentCup, currentCupIdx, move
currentCup = cups[currentCupIdx]
# picks up (removes) cups n+1,n+2,n+3
pickedUp = [cups[safeWrap(currentCupIdx+1)], cups[safeWrap(currentCupIdx+2)], cups[safeWrap(currentCupIdx+3)]]
# print("-- move", move, "--")
# printCups()
# print("picked up:", pickedUp)
[cups.remove(cup) for cup in pickedUp]
# destination cup is current cup label -1
destinationCup = currentCup - 1
# if goes below 1, wrap around to highest
if destinationCup < 0:
# print("now setting to ", totalCups)
destinationCup = totalCups
while True:
# if this has been picked up, keep subtracting 1
if destinationCup in cups:
break
# print(destinationCup, "not present, minusing...")
destinationCup -= 1
# if goes below 1, wrap around to highest
if destinationCup < 0:
# print("now setting to ", totalCups)
destinationCup = totalCups
# print("destination", destinationCup)
# print()
# place cups immediately clockwise of destination cup in same order
destinationIndex = cups.index(destinationCup)
cups = cups[0:destinationIndex+1] + pickedUp + cups[destinationIndex+1:]
# new current cup = current cup + 1 (wraps)
currentCupIdx = cups.index(currentCup)
currentCupIdx = safeWrap(currentCupIdx+1)
move += 1
def getNextNode(idx):
global linkedList
if linkedList.nodeat(idx+1) is None:
return linkedList.first()
return linkedList.nodeat(idx+1)
def getNext(node):
global linkedList
if node.next is None:
return linkedList.nodeat(0)
else:
return node.next
def findAvailableDestination(destination, currentCup):
while True:
destination -= 1
if destination < 1:
destination = max(cupMap.keys())
if destination in cupMap and destination != currentCup.value:
return destination
def part1():
global cups
for i in range(100):
processMove()
startIdx = cups.index(1)
labels = cups[startIdx+1:] + cups[:startIdx]
return "".join(str(cup) for cup in labels)
def part2():
# had to get a fair bit of help on this one
global cups, linkedList, cupsToPick, cupMap
# add the additional numbers preserving order
cups = cups + [cup for cup in range(max(cups)+1, 1000001)]
# create as linked list
linkedList = dllist(cups)
cupMap = {}
# create a hashmap, value -> node in linkedList
for idx in range(linkedList.size):
cupMap[linkedList.nodeat(idx).value] = linkedList.nodeat(idx)
currentCup = linkedList.nodeat(0)
rounds = 10000000
for i in range(rounds):
nextCup = currentCup
for j in range(4):
nextCup = getNext(nextCup)
cupsToPick = []
thisCup = currentCup
for j in range(3):
thisCup = getNext(thisCup)
cupsToPick.append((thisCup, thisCup.value))
for j in range(3):
del cupMap[cupsToPick[j][1]]
linkedList.remove(cupsToPick[j][0])
destination = currentCup.value - 1
if destination in cupsToPick or destination not in cupMap:
destination = findAvailableDestination(destination, currentCup)
destinationNode = cupMap[destination]
nextDestinationNode = destinationNode.next
if nextDestinationNode is None:
destinationNode = None
nextDestinationNode = linkedList.nodeat(0)
linkedList.insert(cupsToPick[0][1], nextDestinationNode)
linkedList.insert(cupsToPick[1][1], nextDestinationNode)
linkedList.insert(cupsToPick[2][1], nextDestinationNode)
if destinationNode is not None:
cupMap[cupsToPick[0][1]] = destinationNode.next
cupMap[cupsToPick[1][1]] = destinationNode.next.next
cupMap[cupsToPick[2][1]] = destinationNode.next.next.next
else:
cupMap[cupsToPick[0][1]] = linkedList.nodeat(0)
cupMap[cupsToPick[1][1]] = linkedList.nodeat(0).next
cupMap[cupsToPick[2][1]] = linkedList.nodeat(0).next.next
currentCup = nextCup
# find position of cup 1
cupOne = cupMap[1]
firstAnswerCup = cupOne.next
if firstAnswerCup is None:
firstAnswerCup = linkedList.nodeat(0)
secondAnswerCup = firstAnswerCup.next
if secondAnswerCup is None:
secondAnswerCup = linkedList.nodeat(0)
return firstAnswerCup.value * secondAnswerCup.value
initialise()
print("Part 1:", part1()) # 76385429
initialise()
print("Part 2:", part2()) # 12621748849