From 4491e98be25bd733740a1f236cd7019e9aa85358 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 2 Dec 2024 10:23:23 +0000 Subject: [PATCH] 2024: d02: ex1: add solution --- 2024/d02/ex1/ex1.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 2024/d02/ex1/ex1.py diff --git a/2024/d02/ex1/ex1.py b/2024/d02/ex1/ex1.py new file mode 100755 index 0000000..053df71 --- /dev/null +++ b/2024/d02/ex1/ex1.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +import itertools +import sys + +Report = list[int] + + +def solve(input: list[str]) -> int: + def parse(input: list[str]) -> list[Report]: + return [[int(n) for n in line.split()] for line in input] + + def is_safe(report: Report) -> bool: + def is_increasing_safe(report: Report): + for a, b in itertools.pairwise(report): + if not 1 <= (b - a) <= 3: + return False + return True + + return is_increasing_safe(report) or is_increasing_safe(report[::-1]) + + return sum(is_safe(report) for report in parse(input)) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()