-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_two.py
64 lines (46 loc) · 1.47 KB
/
part_two.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from itertools import pairwise
from typing import final, override
from infrastructure.solutions.base import Solution
class Year2024Day2Part2Solution(Solution):
@classmethod
@override
def parse_input(cls, text_input: str) -> dict[str, list[list[int]]]:
reports = []
for row in text_input.split('\n'):
if not row:
continue
report = [int(num) for num in row.split()]
reports.append(report)
return {'reports': reports}
@classmethod
@override
def solve(cls, reports: list[list[int]]) -> int:
"""
Time: O(n*m^2)
Space: O(1)
Where n - number of records,
m - maximum length of record
"""
safe_count = 0
for report in reports:
for i in range(len(report)):
if cls.is_safe(report[:i] + report[i + 1:]):
safe_count += 1
break
return safe_count
@classmethod
@final
def is_safe(cls, report: list[int]) -> bool:
is_decreasing = True
is_increasing = True
for x, y in pairwise(report):
# Checking if safe
if not (1 <= abs(x - y) <= 3):
return False
if x > y:
is_increasing = False
if x < y:
is_decreasing = False
return is_decreasing or is_increasing
if __name__ == '__main__':
print(Year2024Day2Part2Solution.main())