This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
23_crab_cups.py
68 lines (47 loc) · 1.45 KB
/
23_crab_cups.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
#############################
# --- Day 23: Crab Cups --- #
#############################
import AOCUtils
class Node:
def __init__(self, data):
self.data = data
self.next = None
def crabCups(cups, moves):
nodes = [Node(k) for k in cups]
for i in range(len(cups)-1):
nodes[i].next = nodes[i+1]
nodes[len(cups)-1].next = nodes[0]
nodeLookup = {node.data: node for node in nodes}
cur = nodes[0].data
for _ in range(moves):
p = nodeLookup[cur]
a = nodeLookup[cur].next
b = nodeLookup[cur].next.next
c = nodeLookup[cur].next.next.next
nodeLookup[cur].next = c.next
dest = cur
while True:
dest -= 1
if dest < 1:
dest = len(cups)
if dest not in [a.data, b.data, c.data]:
break
c.next = nodeLookup[dest].next
nodeLookup[dest].next = a
nodeLookup[dest].next.next = b
nodeLookup[dest].next.next.next = c
cur = nodeLookup[cur].next.data
return nodeLookup[1].next
#############################
rawCups = AOCUtils.loadInput(23)
cups1 = [int(i) for i in str(rawCups)]
p = crabCups(cups1, 100)
p1 = []
for _ in range(8):
p1.append(str(p.data))
p = p.next
print("Part 1: {}".format("".join(p1)))
cups2 = cups1 + list(range(len(cups1)+1, 1000000+1))
p = crabCups(cups2, 10000000)
print("Part 2: {}".format(p.data * p.next.data))
AOCUtils.printTimeTaken()