-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday2.py
More file actions
65 lines (50 loc) · 2.21 KB
/
day2.py
File metadata and controls
65 lines (50 loc) · 2.21 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def calcsafe():
with open("day2input.txt") as file:
lines = file.readlines()
safe = 0
for i in range(len(lines)):
oneNumber = True
# Clean and split the line into integers
line = [int(x.strip()) for x in lines[i].split()]
# Skip lines with fewer than 2 numbers
if len(line) < 2:
continue
# Compute differences
diff = [line[j + 1] - line[j] for j in range(len(line) - 1)]
# Check if all differences are increasing or decreasing
if all(d > 0 for d in diff) or all(d < 0 for d in diff):
# Check if all differences are within the safe range
safeish = all(1 <= abs(d) <= 3 for d in diff)
if safeish:
safe += 1
elif oneNumber:
oneNumber = False
return safe
def calcsafeAdj():
with open("day2input.txt") as file:
lines = file.readlines()
safe = 0
for i in range(len(lines)):
# Clean and split the line into integers
line = [int(x.strip()) for x in lines[i].split()]
# Skip lines with fewer than 2 numbers
if len(line) < 2:
continue
# Function to check if a sequence is safe
def is_safe(sequence):
# Compute differences
diff = [sequence[j + 1] - sequence[j] for j in range(len(sequence) - 1)]
# Check if all differences are increasing or decreasing and within range
return (all(d > 0 for d in diff) or all(d < 0 for d in diff)) and all(1 <= abs(d) <= 3 for d in diff)
# Check if the original line is safe
if is_safe(line):
safe += 1
continue
# Check if removing one level makes it safe
for j in range(len(line)):
modified_line = line[:j] + line[j+1:] # Remove the j-th level
if is_safe(modified_line):
safe += 1
break # No need to check further removals for this line
return safe
print(calcsafeAdj())