2019: d16: ex2: add solution

This commit is contained in:
Bruno BELANYI 2019-12-16 14:34:26 +01:00
parent 9e0b69d56e
commit 576769e648

27
2019/d16/ex2/ex2.py Executable file
View file

@ -0,0 +1,27 @@
#!/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()