adventofcode2022/day20/sol.py

45 lines
902 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)
idx = list(range(len(data)))
2022-12-20 06:31:14 +00:00
N = len(data) - 1
2022-12-20 05:34:48 +00:00
for _ in range(rounds):
for k in range(len(data)):
i = idx.index(k)
2022-12-20 06:31:14 +00:00
x = data[k]
2022-12-20 05:34:48 +00:00
if x != 0:
2022-12-20 06:31:14 +00:00
j = (i+x) % N
2022-12-20 05:34:48 +00:00
if x < 0 and j == 0:
2022-12-20 06:31:14 +00:00
j = N
2022-12-20 05:34:48 +00:00
idx.insert(j, idx.pop(i))
else:
j = i
2022-12-20 06:31:14 +00:00
#print(x, i, j, [data[k] for k in idx])
2022-12-20 05:34:48 +00:00
2022-12-20 06:31:14 +00:00
return [data[k] for k in idx]
2022-12-20 05:34:48 +00:00
def answer(data):
i = data.index(0)
2022-12-20 06:31:14 +00:00
n = []
2022-12-20 05:34:48 +00:00
for j in 1000, 2000, 3000:
2022-12-20 06:31:14 +00:00
n.append( data[(i+j)%len(data)] )
print(n)
return sum(n)
2022-12-20 05:34:48 +00:00
2022-12-20 06:31:14 +00:00
#print(data)
2022-12-20 05:34:48 +00:00
copy = mix(data)
print(answer(copy))
p2 = [x*811589153 for x in data]
print(answer(mix(p2, 10)))