Compare commits
3 commits
b5425025a6
...
08aab425a2
Author | SHA1 | Date | |
---|---|---|---|
Bruno BELANYI | 08aab425a2 | ||
Bruno BELANYI | 66c9d92749 | ||
Bruno BELANYI | 6634348afe |
31
2024/d02/ex1/ex1.py
Executable file
31
2024/d02/ex1/ex1.py
Executable file
|
@ -0,0 +1,31 @@
|
||||||
|
#!/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 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()
|
39
2024/d02/ex2/ex2.py
Executable file
39
2024/d02/ex2/ex2.py
Executable file
|
@ -0,0 +1,39 @@
|
||||||
|
#!/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 not 1 <= (b - a) <= 3:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
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()
|
1000
2024/d02/ex2/input
Normal file
1000
2024/d02/ex2/input
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue