-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
47 lines (41 loc) · 1.23 KB
/
day2.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
45
46
47
def pass_sorting(record: list) -> bool:
return (
True
if sorted(record) == record or sorted(record, reverse=True) == record
else False
)
def pass_distance(record: list) -> bool:
data_list = record
result = True
while len(data_list) >= 2:
if (
abs(data_list[0] - data_list[1]) >= 1
and abs(data_list[0] - data_list[1]) <= 3
):
result = True
else:
result = False
break
data_list = data_list[1:]
return result
def main() -> int:
formatted_list = []
# read from input
with open("./day2/input", "r") as file:
for line in file:
formatted_list.append(line.rstrip().split(" "))
safe = 0
for record in formatted_list:
int_record = [int(x) for x in record]
result = pass_sorting(int_record) and pass_distance(int_record)
if result is True:
safe += 1
else:
for idx in range(len(int_record)):
a = int_record[:idx] + int_record[idx + 1 :]
test = pass_sorting(a) and pass_distance(a)
if test is True:
safe += 1
break
return safe
print(main())