From 2646a1609d6b8eaff48c34ea9d0fdfbdcaf7b0e6 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..7895e2b --- /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 zip(windowed, windowed[1:])) + + +def main() -> None: + input = [int(line) for line in sys.stdin.readlines()] + print(solve(input)) + + +if __name__ == "__main__": + main()