-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
41 lines (31 loc) · 1.09 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
from utils import parse_file_to_int_list
# --- Day 2: Red-Nosed Reports ---
def decreasing(lst):
return all(
(current > next_elem) and (1 <= (current - next_elem) <= 3)
for current, next_elem in zip(lst, lst[1:])
) if len(lst) > 1 else True
def increasing(lst):
return all(
(current < next_elem) and (1 <= abs(current - next_elem) <= 3)
for current, next_elem in zip(lst, lst[1:])
) if len(lst) > 1 else True
def reports():
all_lst = parse_file_to_int_list('./input/day2.txt')
count = sum(map(lambda x: 1 if decreasing(x) or increasing(x) else 0, all_lst))
return count
print('part1', reports())
def reports2():
all_lst = parse_file_to_int_list('./input/day2.txt')
count = 0
for lst in all_lst:
if decreasing(lst) or increasing(lst):
count += 1
else:
for i in range(len(lst)):
removed_lst = lst[:i] + lst[i+1:]
if decreasing(removed_lst) or increasing(removed_lst):
count += 1
break
return count
print('part2', reports2())