-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp02.py
45 lines (36 loc) · 1.11 KB
/
p02.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
from itertools import pairwise
from aocd import data
def is_bad(l1, l2, direction):
if l1 == l2 or abs(l1 - l2) > 3:
return True
elif direction == "increasing" and l1 - l2 > 0:
return True
elif direction == "decreasing" and l1 - l2 < 0:
return True
else:
return False
safe = 0
almost_safe = 0
for report in data.splitlines():
levels = list(map(int, report.split()))
diffs = [l1 - l2 for l1, l2 in pairwise(levels)]
if (all(d > 0 and abs(d) <= 3 for d in diffs)
or all(d < 0 and abs(d) <= 3 for d in diffs)):
safe += 1
continue
neg = [d for d in diffs if d < 0]
pos = [d for d in diffs if d > 0]
if len(neg) > len(pos):
direction = "increasing"
else:
direction = "decreasing"
for r in range(len(levels)):
problem_dampened_report = levels[:r] + levels[r+1:]
for l1, l2 in pairwise(problem_dampened_report):
if is_bad(l1, l2, direction):
break
else:
almost_safe += 1
break
print("Part 1:", safe)
print("Part 2:", safe + almost_safe)