From 704cf451cbac9738e187bfb91abe244a56c46059 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 16 Dec 2019 14:34:26 +0100 Subject: [PATCH] 2019: d16: ex2: add solution --- 2019/d16/ex2/ex2.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 2019/d16/ex2/ex2.py diff --git a/2019/d16/ex2/ex2.py b/2019/d16/ex2/ex2.py new file mode 100755 index 0000000..a13a0bf --- /dev/null +++ b/2019/d16/ex2/ex2.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python + +import sys +from functools import reduce +from itertools import chain, cycle +from typing import Iterable, List + + +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()