-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.b.py
39 lines (33 loc) · 906 Bytes
/
02.b.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
with open("2024/02.input.txt", encoding="utf-8") as file:
data = file.read()
lines = [[int(num) for num in line.split()] for line in data.splitlines()]
def check_safe(line: list[int]) -> bool:
for i in range(len(line) - 1):
if (
line[i] < line[i + 1]
or abs(line[i] - line[i + 1]) < 1
or abs(line[i] - line[i + 1]) > 3
):
break
else:
return True
for i in range(len(line) - 1):
if (
line[i] > line[i + 1]
or abs(line[i] - line[i + 1]) < 1
or abs(line[i] - line[i + 1]) > 3
):
break
else:
return True
return False
safe = 0
for line in lines:
if check_safe(line):
safe += 1
continue
for i in range(len(line)):
if check_safe(line[:i] + line[i + 1 :]):
safe += 1
break
print(safe)