-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.py
62 lines (46 loc) · 1.74 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
55
56
57
58
59
60
61
62
with open("./input.txt") as f:
data = f.read().splitlines()
data = list(map(int, list(data[0])))
class Node:
def __init__(self, v):
self.next = None
self.value = v
def __repr__(self):
return str(self.value)
class CircularLinkedList:
def __init__(self, data):
self.nodes = {} # value - node mapping
nodes = [Node(v) for v in data]
for i, v in enumerate(data):
nodes[i].next = nodes[(i + 1) % len(data)]
self.nodes[nodes[i].value] = nodes[i]
def get_insert_index(current_cup, selected_cups):
selected_values = list(map(lambda x: x.value, selected_cups))
target_value = current_cup.value
while target_value in selected_values or target_value == current_cup.value:
target_value -= 1
if target_value <= 0:
target_value = len(data)
return clist.nodes[target_value]
def solve(steps):
current_cup = clist.nodes[data[0]]
for i in range(steps):
selected_cups = [current_cup.next, current_cup.next.next, current_cup.next.next.next]
destination = get_insert_index(current_cup, selected_cups)
current_cup.next = selected_cups[-1].next
selected_cups[-1].next = destination.next
destination.next = selected_cups[0]
current_cup = current_cup.next
clist = CircularLinkedList(data)
solve(100)
one_node = clist.nodes[1]
answer = ""
while one_node.next != clist.nodes[1]:
answer += str(one_node.next.value)
one_node = one_node.next
print(answer)
data = data + list(range(len(data)+1, 1000001))
clist = CircularLinkedList(data)
solve(10000000)
one_node = clist.nodes[1]
print(one_node.next.value * one_node.next.next.value)