2024: d02: ex1: add solution
This commit is contained in:
parent
a1f19ad4da
commit
b8f01de0d3
33
2024/d02/ex1/ex1.py
Executable file
33
2024/d02/ex1/ex1.py
Executable file
|
@ -0,0 +1,33 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import itertools
|
||||||
|
|
||||||
|
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 a >= b:
|
||||||
|
return False
|
||||||
|
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()
|
Loading…
Reference in a new issue