-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.py
More file actions
35 lines (27 loc) · 802 Bytes
/
Copy pathday02.py
File metadata and controls
35 lines (27 loc) · 802 Bytes
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
import numpy as np
filepath = "day02.txt"
def load_data(filepath):
with open(filepath) as file:
return [np.array(list(map(int, row.split()))) for row in file]
def is_safe(nums):
diffs = np.diff(nums)
if (diffs > 0).all() and (diffs <= 3).all():
return True
if (diffs < 0).all() and (diffs >= -3).all():
return True
return False
def solve_part_i():
data = load_data(filepath)
result = sum(is_safe(nums) for nums in data)
print("Part I:", result)
def solve_part_ii():
data = load_data(filepath)
result = 0
for nums in data:
if is_safe(nums):
result += 1
else:
result += any(is_safe(np.delete(nums, i)) for i in range(len(nums)))
print("Part II", result)
solve_part_i()
solve_part_ii()