AdventOfCode/2019/day16.py

56 lines
1.7 KiB
Python
Raw Normal View History

2019-12-16 07:42:11 +01:00
import math
2019-12-17 02:21:47 +01:00
import sys
2019-12-16 07:42:11 +01:00
def main():
with open("day16.txt", "rt") as input_file:
inputs = [int(i) for i in input_file.read().rstrip()]
2019-12-17 00:24:34 +01:00
# Part 1
2019-12-16 07:54:07 +01:00
result = phaze2phaze(inputs)
2019-12-17 00:24:34 +01:00
print(f"First 8 digits: {''.join(str(int(i)) for i in result[:8])}")
2019-12-16 07:54:07 +01:00
2019-12-17 00:24:34 +01:00
# Part 2
2019-12-17 02:21:47 +01:00
offset = int(''.join(str(i) for i in inputs[:7]))
rev_inputs = [i for _ in range(10000) for i in reversed(inputs)]
rev_offset = len(rev_inputs) - offset
2019-12-17 00:24:34 +01:00
2019-12-17 02:21:47 +01:00
print(f"Offset: {offset}. Reversed offset: {rev_offset}")
result = phaze4phaze(rev_inputs, index_limit=rev_offset + 8)
result_slice = result[rev_offset-8:rev_offset]
result_slice.reverse()
print(f"Answer: {''.join(str(int(i)) for i in result_slice)}")
2019-12-17 00:24:34 +01:00
def p(index, pos):
return round(math.cos(((pos - index + 1) // index) * (math.pi / 2)))
2019-12-17 02:21:47 +01:00
def phaze2phaze(inputs, rounds=100):
2019-12-17 00:24:34 +01:00
""" https://www.youtube.com/watch?v=i0leFvyLEF8 """
for phase_id in range(rounds):
2019-12-17 02:21:47 +01:00
print_phaze(phase_id + 1)
2019-12-16 07:42:11 +01:00
outputs = [None] * len(inputs)
2019-12-17 02:21:47 +01:00
for index in range(len(inputs)):
2019-12-17 00:24:34 +01:00
out = sum(x * p(index + 1, pos + index) for pos, x in enumerate(inputs[index:]))
2019-12-16 07:42:11 +01:00
outputs[index] = abs(math.fmod(out, 10))
inputs = outputs
2019-12-16 07:54:07 +01:00
return inputs
2019-12-16 07:42:11 +01:00
2019-12-17 02:21:47 +01:00
def phaze4phaze(rev_inputs, index_limit):
for phase_id in range(100):
print_phaze(phase_id + 1)
outputs = [None] * index_limit
sumsum = 0
for index in range(index_limit):
sumsum += rev_inputs[index]
outputs[index] = abs(math.fmod(sumsum, 10))
rev_inputs = outputs
return rev_inputs
def print_phaze(n):
print(f"{n}... ", end="")
sys.stdout.flush()
2019-12-16 07:42:11 +01:00
if __name__ == "__main__":
main()