-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.py
50 lines (39 loc) · 1.24 KB
/
2.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
50
"""Day 1 of Advent of Code 2024"""
def safe_data(data)->bool:
"""Function to check if data is safe."""
safe = True
if data == sorted(data) or data == sorted(data, reverse=True):
for j in range(1,len(data)):
diff = abs(data[j]-data[j-1])
if diff > 3 or diff == 0:
safe = False
else:
safe = False
return safe
def part1():
with open("./input/2", 'r') as f:
lines = f.readlines()
score = 0
for i in range(len(lines)):
data = [int(s) for s in lines[i].split()]
safe = safe_data(data)
if safe == True:
score += 1
return score
def part2():
with open("./input/2", 'r') as f:
lines = f.readlines()
score = 0
for i in range(len(lines)):
data = [int(s) for s in lines[i].split()]
# Check every possible vector with one value removed to see if safe.
for k in range(len(data)):
partial_data = data[:k] + data[k+1:]
safe = safe_data(partial_data)
if safe == True:
score +=1
break
return score
if __name__ == "__main__":
print(f"Answer part 1: {part1()}")
print(f"Answer part 2: {part2()}")