diff --git a/2019/d16/ex2/ex2.py b/2019/d16/ex2/ex2.py new file mode 100755 index 0000000..317d157 --- /dev/null +++ b/2019/d16/ex2/ex2.py @@ -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()