Added day 16

master
Tibor Bizjak 2023-03-14 22:14:48 +01:00
parent da8778d527
commit 85846d079f
1 changed files with 43 additions and 0 deletions

43
day16.py 100644
View File

@ -0,0 +1,43 @@
#!/usr/bin/env python3
from intcode import defaultlist
from operator import mul
def str_of_signal(signal):
return ''.join(map(str, signal))
def apply_pattern(signal, offset=0):
sums = [None] * (len(signal)-1) + [signal[-1]]
for i in range(len(signal)-2, -1, -1):
sums[i] = sums[i+1] + signal[i]
sums = defaultlist(sums, lambda : 0)
new = []
for i in range(len(signal)):
step = i + offset + 1
n = 0
for block in range(i, len(signal), 4*step):
n += sums[block] - sums[block + step]
n -= sums[block + 2*step] - sums[block + 3*step]
new.append(abs(n)%10)
return new
def phase(signal, n, offset=0):
signal = signal[offset:]
for i in range(n):
signal = apply_pattern(signal, offset)
return signal
def preproc(puzzle_input):
return list(map(int, puzzle_input))
def partI(signal):
return str_of_signal(phase(signal, 100)[:8])
def partII(signal):
signal = signal * 10**4
offset = int(str_of_signal(signal[:7]))
return str_of_signal(phase(signal, 100, offset)[:8])