-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_01.py
44 lines (36 loc) · 1.28 KB
/
task_01.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
class Task01:
RISING: str = "rising"
FALLING: str = "falling"
@classmethod
def solve(cls, file_content: list[str]) -> int:
safe_report_count = 0
for report in file_content:
levels_txt = report.split()
levels = [int(level) for level in levels_txt]
safe_report_count = safe_report_count + 1 if cls.is_safe(levels=levels) else safe_report_count
return safe_report_count
@classmethod
def is_safe(cls, levels: list[int]) -> bool:
# work out report trajectory
first = levels[0]
second = levels[1]
if first == second:
return False
if first > second:
trajectory = cls.FALLING
else:
trajectory = cls.RISING
# process report
i = 1
while i < len(levels):
previous_number = levels[i - 1]
current_number = levels[i]
difference = previous_number - current_number
if difference == 0:
return False
if trajectory == cls.FALLING and (difference < 1 or difference > 3):
return False
if trajectory == cls.RISING and (difference < -3 or difference > -1):
return False
i += 1
return True