From 8ad65f864f9f34ce0e9663c0509d9be79ef1e37f Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 16 Dec 2019 11:37:47 +0100 Subject: [PATCH] 2019: d16: ex1: add solution --- 2019/d16/ex1/ex1.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 2019/d16/ex1/ex1.py diff --git a/2019/d16/ex1/ex1.py b/2019/d16/ex1/ex1.py new file mode 100755 index 0000000..0d8d731 --- /dev/null +++ b/2019/d16/ex1/ex1.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 sequencer(pattern: List[int], n: int) -> Iterable[int]: + gen = cycle(list(chain(*([a] * n for a in pattern)))) + next(gen) # Skip the first one + yield from gen + + +def main() -> None: + signal = [int(d) for d in sys.stdin.read().strip()] + base_pattern = [0, 1, 0, -1] + + for __ in range(100): + signal = [ + abs(sum(a * b for a, b in zip(signal, sequencer(base_pattern, i + 1)))) % 10 + for i in range(len(signal)) + ] + + print(reduce(lambda a, b: a * 10 + b, signal[0:8])) + + +if __name__ == "__main__": + main()