-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_2.py
31 lines (22 loc) · 775 Bytes
/
2_2.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
with open("input/2_2.txt") as f:
reports = [list(map(int, line.split())) for line in f]
def is_safe(report: list, ignore_idx: int) -> bool:
descending = False
ascending = False
previous = None
for idx, level in enumerate(report):
if idx == ignore_idx:
continue
if not previous:
previous = level
continue
if previous < level:
ascending = True
elif previous > level:
descending = True
if (previous == level) or abs(previous - level) > 3 or (ascending and descending):
return False
previous = level
return True
safe_reports = sum(any(is_safe(report, i) for i in range(len(report))) for report in reports)
print(safe_reports)