-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2_t1.py
31 lines (26 loc) · 853 Bytes
/
d2_t1.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
#!/bin/python
def is_safe(arr):
updown = {'up': 0, 'down': 0, 'eq': 0}
is_safe_by_delta = False
for i in range(1, len(arr)):
delta = arr[i] - arr[i - 1]
# print(f"Delta: {delta}")
if abs(delta) > 0 and abs(delta) < 4: # 2 cmp are suboptimal, but best I can do now
is_safe_by_delta = True
else:
return False
if delta < 0:
updown['down'] += 1
elif delta > 0:
updown['up'] += 1
else:
updown['eq'] += 1
return is_safe_by_delta and (updown['up'] == len(arr)-1 or updown['down'] == len(arr)-1)
safe_reports_cnt = 0
with open('input.txt', 'r') as f:
reports = f.readlines()
for r in reports:
arr = list(map(int, r.split()))
if is_safe(arr):
safe_reports_cnt += 1
print(safe_reports_cnt)