2024: d02: ex2: add solution

This commit is contained in:
Bruno BELANYI 2024-12-02 10:24:02 +00:00
parent 78ff7514f8
commit 28a945c421

36
2024/d02/ex2/ex2.py Executable file
View file

@ -0,0 +1,36 @@
#!/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) -> bool:
return all(1 <= (b - a) <= 3 for a, b in itertools.pairwise(report))
return is_increasing_safe(report) or is_increasing_safe(report[::-1])
# Dampening by brute force
def is_dampened_safe(report: Report) -> bool:
for i in range(len(report)):
dampened_report = report[:i] + report[i + 1 :]
if is_safe(dampened_report):
return True
return False
return sum(is_dampened_safe(report) for report in parse(input))
def main() -> None:
input = sys.stdin.read().splitlines()
print(solve(input))
if __name__ == "__main__":
main()