From 9f280f2e3d15d8481f15e8e08e8cd7fa86559caa Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Wed, 1 Dec 2021 10:43:55 +0100 Subject: [PATCH] 2021: d01: ex2: add solution --- 2021/d01/ex2/ex2.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100755 2021/d01/ex2/ex2.py diff --git a/2021/d01/ex2/ex2.py b/2021/d01/ex2/ex2.py new file mode 100755 index 0000000..2ef9e60 --- /dev/null +++ b/2021/d01/ex2/ex2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import collections +import itertools +import sys +from typing import List + + +def sliding_window(iterable, n): + it = iter(iterable) + window = collections.deque(itertools.islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) + + +def solve(input: List[int]) -> int: + windowed = [sum(window) for window in sliding_window(input, 3)] + return sum(prev < cur for (prev, cur) in itertools.pairwise(windowed)) + + +def main() -> None: + input = [int(line) for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()