-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp20.py
129 lines (109 loc) · 2.81 KB
/
p20.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
# https://adventofcode.com/2022/day/20
# Created by: Menaka S. 20 Dec 2022
import sys
allnodes = []
head = None
curr = None
class Node:
# Constructor to create a new node
def __init__(self, num):
self.num = num
self.nxt = None
self.prv = None
allnodes.append(self)
def insert(self, num):
newn = Node(num)
self.nxt = newn
newn.prv = self
newn.nxt = head
head.prv = newn
return newn
def move_nxt(self, dir,ishead):
node = self
node.prv.nxt = node.nxt
node.nxt.prv = node.prv
if dir:
prv = node.nxt
nxt = node.nxt.nxt
else:
prv = node.prv.prv
nxt = node.prv
prv.nxt = node
node.prv = prv
nxt.prv = node
node.nxt = nxt
if dir and ishead:
return self.prv
elif not dir and ishead:
return self.nxt
else:
return self
def parse_input():
global head,curr
first = 1
for line in sys.stdin:
line = line.strip()
if first:
head = Node(int(line))
curr = head
first = 0
else:
curr = curr.insert(int(line))
def mix_once():
global head,curr
for i in range(len(allnodes)):
times = (allnodes[i].num)%(len(allnodes)-1)
curr = allnodes[i]
for i in range(0,abs(times)):
if times > 0:
if head == curr:
head = curr.move_nxt(1,1)
else:
curr = curr.move_nxt(1,0)
else:
if head == curr:
head = curr.move_nxt(0,1)
else:
curr = curr.move_nxt(0,0)
def printit():
global head
curr = head
for j in range(len(allnodes)):
print(curr.num, end=' ')
curr = curr.nxt
print("\n")
def part(part):
global head
if part == 2:
dkey = 811589153
curr = head
for j in range(len(allnodes)):
curr = allnodes[j]
curr.nxt = allnodes[(j+1) %len(allnodes)]
curr.prv = allnodes[j-1]
curr.num *=dkey
for i in range(9):
mix_once()
mix_once()
curr = head
ind0 = -1
ct = 0
for j in range(len(allnodes)):
if not curr.num:
ind0 = ct
ct +=1
curr = curr.nxt
indices= []
for i in [1000,2000,3000]:
indices.append((ind0 + i)%len(allnodes))
#printit()
#print(ind0,indices)
total = 0
for j in range(0,len(allnodes)):
if j in indices:
total+=curr.num
curr = curr.nxt
return total
parse_input()
print(part(1))
print(part(2))