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()