-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.py
32 lines (27 loc) · 835 Bytes
/
part2.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
def solve(input_srt):
lines = input_srt.strip().split("\n")
reports = [[int(num) for num in line.split()] for line in lines]
safe_count = 0
for report in reports:
if is_safe(report):
safe_count += 1
else:
for i in range(len(report)):
modified_report = report[:i] + report[i+1:]
if is_safe(modified_report):
print(report)
break
return str(safe_count)
def is_safe(report):
diffs = []
for i in range(len(report) - 1):
diff = report[i + 1] - report[i]
if diff == 0:
return False
diffs.append(diff)
if all(1 <= d <= 3 for d in diffs):
return True
elif all(-3 <= d <= -1 for d in diffs):
return True
else:
return False