adventofcode2022/day20/sol.py

47 lines
938 B
Python
Raw Normal View History

2022-12-20 05:34:48 +00:00
data = []
for line in open("input"):
data.append(int(line.strip()))
#data = [1, 2, -3, 3, -2, 0, 4, ]
def mix(data, rounds=1):
#assert len(set(data)) == len(data)
copy = list(data)
idx = list(range(len(data)))
for _ in range(rounds):
for k in range(len(data)):
i = idx.index(k)
x = copy[i]
if x != 0:
j = (i+x) % (len(copy)-1)
if x < 0 and j == 0:
j = len(copy)-1
copy.insert(j, copy.pop(i))
idx.insert(j, idx.pop(i))
else:
j = i
#print(x, i, j, copy)
return copy
def answer(data):
i = data.index(0)
n = 0
for j in 1000, 2000, 3000:
x = data[(i+j)%len(copy)]
#print(x)
n += x
return n
#print(copy)
copy = mix(data)
print(answer(copy))
p2 = [x*811589153 for x in data]
print(answer(mix(p2, 10)))