advent-of-code/2019/d16/ex2/ex2.py
Bruno BELANYI f9fc9fbd6b treewide: fix 'ruff check' errors
This is mostly about unused imports.

A couple errors remain, but are fine in my book (using `l` as a variable
name, assigning a lambda to a variable).
2024-11-23 19:24:55 +00:00

28 lines
855 B
Python
Executable file

#!/usr/bin/env python
import sys
from functools import reduce
def main() -> None:
rep = 10000
signal = [int(d) for d in sys.stdin.read().strip()] * rep
offset = reduce(lambda a, b: a * 10 + b, signal[0:7])
assert offset >= len(signal) / 2 # Sanity check
# The trick is that the second half is only affected by itself (triangular matrix):
# For i > len(signal) / 2, new_signal[i] = sum(signal, i, len(signal))
# Therefore, we're only interested in numbers that start at the offset
signal = signal[offset:] # Only take the end we need
for __ in range(100):
for i in range(len(signal) - 1, 0, -1): # Do the sum from the end
signal[i - 1] += signal[i]
signal[i - 1] = signal[i - 1] % 10
print(reduce(lambda a, b: a * 10 + b, signal[:8]))
if __name__ == "__main__":
main()