Day 16: 10s for part 1

This commit is contained in:
Dece 2019-12-17 00:24:34 +01:00
parent 5bde897f48
commit 8b95bd4991

View file

@ -4,31 +4,35 @@ import math
def main(): def main():
with open("day16.txt", "rt") as input_file: with open("day16.txt", "rt") as input_file:
inputs = [int(i) for i in input_file.read().rstrip()] inputs = [int(i) for i in input_file.read().rstrip()]
inputs_orig = inputs.copy() # inputs = [1,2,3,4,5,6,7,8]
# inputs = [0,3,0,3,6,7,3,2,5,7,7,2,1,2,9,4,4,0,6,3,4,9,1,5,6,5,4,7,4,6,6,4]
# result = phaze2phaze(inputs) # Part 1
# print(f"First 8 digits: {''.join(str(int(i)) for i in result[:8])}") result = phaze2phaze(inputs)
print(f"First 8 digits: {''.join(str(int(i)) for i in result[:8])}")
# Part 2 # Part 2
offset = int(''.join(str(i) for i in inputs[:7])) # offset = int(''.join(str(i) for i in inputs[:7]))
inputs = [i for _ in range(10000) for i in inputs_orig] # rev_inputs = [i for _ in range(10000) for i in reversed(inputs)]
result = phaze2phaze(inputs) # rev_offset = len(rev_inputs) - offset
print(f"8 digits at offset: {''.join(str(int(i)) for i in result[offset:offset + 8])}")
# print(f"Offset: {offset}. Reversed offset: {rev_offset}")
# result = phaze2phaze(rev_inputs, index_limit=rev_offset)
# result_slice = result[rev_offset - 8:rev_offset]
# assert result_slice == [8,4,4,6,2,0,2,6]
# print(f"8 digits at offset: {''.join(str(int(i)) for i in result_slice)}")
def phaze2phaze(inputs): def p(index, pos):
p = [0, 1, 0, -1] return round(math.cos(((pos - index + 1) // index) * (math.pi / 2)))
for phase_id in range(100):
def phaze2phaze(inputs, rounds=100, index_limit=None):
""" https://www.youtube.com/watch?v=i0leFvyLEF8 """
for phase_id in range(rounds):
phase_id += 1 phase_id += 1
print(f"Phase {phase_id}...")
outputs = [None] * len(inputs) outputs = [None] * len(inputs)
for index in range(len(inputs)): print(f"Phase {phase_id}... ")
ex_f = index + 1 for index in range(index_limit or len(inputs)):
ex_p = [p[i // ex_f] for i in range(len(p) * ex_f)] out = sum(x * p(index + 1, pos + index) for pos, x in enumerate(inputs[index:]))
while len(ex_p) < len(inputs) + 1:
ex_p += ex_p # yikes
ex_p = ex_p[1:len(inputs) + 1]
out = sum(a * b for a, b in zip(inputs, ex_p))
outputs[index] = abs(math.fmod(out, 10)) outputs[index] = abs(math.fmod(out, 10))
inputs = outputs inputs = outputs
return inputs return inputs
@ -36,4 +40,3 @@ def phaze2phaze(inputs):
if __name__ == "__main__": if __name__ == "__main__":
main() main()