generated from alisonrclarke/advent-of-code-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.py
49 lines (37 loc) · 1.34 KB
/
day02.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
48
49
import sys
import utils
test_mode = len(sys.argv) > 1
if test_mode:
suffix = sys.argv[2] if len(sys.argv) > 2 else ""
input_file = f"day02_test_input{suffix}.txt"
else:
input_file = f"day02_input.txt"
data = utils.input_as_lines(input_file)
safe_count = 0
for line in data:
levels = [int(s) for s in line.split()]
all_inc = all(levels[i] < levels[i + 1] for i in range(len(levels) - 1))
all_dec = all(levels[i] > levels[i + 1] for i in range(len(levels) - 1))
if all_inc or all_dec:
safe = all(abs(levels[i] - levels[i + 1]) < 4 for i in range(len(levels) - 1))
if safe:
safe_count += 1
print(f"Part 1: {safe_count}")
safe_count = 0
for line in data:
levels = [int(s) for s in line.split()]
remove_index = 0
all_levels = levels
while remove_index <= len(levels) + 1:
all_inc = all(levels[i] < levels[i + 1] for i in range(len(levels) - 1))
all_dec = all(levels[i] > levels[i + 1] for i in range(len(levels) - 1))
if all_inc or all_dec:
safe = all(
abs(levels[i] - levels[i + 1]) < 4 for i in range(len(levels) - 1)
)
if safe:
safe_count += 1
break
levels = all_levels[:remove_index] + all_levels[remove_index + 1 :]
remove_index += 1
print(f"Part 2: {safe_count}")