Day 16 part 2

This commit is contained in:
Dece 2019-12-17 02:21:47 +01:00
parent 8b95bd4991
commit 14e26cafc6

View file

@ -1,42 +1,55 @@
import math import math
import sys
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 = [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]
# Part 1 # Part 1
result = phaze2phaze(inputs) result = phaze2phaze(inputs)
print(f"First 8 digits: {''.join(str(int(i)) for i in result[:8])}") 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]))
# rev_inputs = [i for _ in range(10000) for i in reversed(inputs)] rev_inputs = [i for _ in range(10000) for i in reversed(inputs)]
# rev_offset = len(rev_inputs) - offset rev_offset = len(rev_inputs) - offset
# print(f"Offset: {offset}. Reversed offset: {rev_offset}") print(f"Offset: {offset}. Reversed offset: {rev_offset}")
# result = phaze2phaze(rev_inputs, index_limit=rev_offset) result = phaze4phaze(rev_inputs, index_limit=rev_offset + 8)
# result_slice = result[rev_offset - 8:rev_offset] result_slice = result[rev_offset-8:rev_offset]
# assert result_slice == [8,4,4,6,2,0,2,6] result_slice.reverse()
# print(f"8 digits at offset: {''.join(str(int(i)) for i in result_slice)}") print(f"Answer: {''.join(str(int(i)) for i in result_slice)}")
def p(index, pos): def p(index, pos):
return round(math.cos(((pos - index + 1) // index) * (math.pi / 2))) return round(math.cos(((pos - index + 1) // index) * (math.pi / 2)))
def phaze2phaze(inputs, rounds=100, index_limit=None): def phaze2phaze(inputs, rounds=100):
""" https://www.youtube.com/watch?v=i0leFvyLEF8 """ """ https://www.youtube.com/watch?v=i0leFvyLEF8 """
for phase_id in range(rounds): for phase_id in range(rounds):
phase_id += 1 print_phaze(phase_id + 1)
outputs = [None] * len(inputs) outputs = [None] * len(inputs)
print(f"Phase {phase_id}... ") for index in range(len(inputs)):
for index in range(index_limit or len(inputs)):
out = sum(x * p(index + 1, pos + index) for pos, x in enumerate(inputs[index:])) out = sum(x * p(index + 1, pos + index) for pos, x in enumerate(inputs[index:]))
outputs[index] = abs(math.fmod(out, 10)) outputs[index] = abs(math.fmod(out, 10))
inputs = outputs inputs = outputs
return inputs return inputs
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()
if __name__ == "__main__": if __name__ == "__main__":
main() main()