Skip to content

Commit a6d6844

Browse files
committedDec 3, 2024
Day 2 - part 1
1 parent f5b4616 commit a6d6844

File tree

3 files changed

+1049
-0
lines changed

3 files changed

+1049
-0
lines changed
 

‎2024/day02/input

+1,000
Large diffs are not rendered by default.

‎2024/day02/solution.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/python3
2+
3+
def parse_input(path:str) -> list[tuple[int]]:
4+
with open(path) as file:
5+
data = file.read().splitlines()
6+
7+
reports = [tuple(int(i) for i in line.split()) for line in data]
8+
9+
return reports
10+
11+
positive = lambda n: True if n > 0 else False
12+
13+
def count_safe(reports: list[tuple[int]]) -> int:
14+
safe = 0
15+
16+
for report in reports:
17+
prev = report[0]
18+
sign = None
19+
20+
for pos in report[1:]:
21+
dif = pos-prev
22+
23+
if abs(dif) > 3:
24+
break
25+
elif dif == 0:
26+
break
27+
elif positive(dif) != sign and sign is not None:
28+
break
29+
30+
prev = pos
31+
sign = positive(dif)
32+
else:
33+
safe += 1
34+
35+
return safe
36+
37+
if __name__ == '__main__':
38+
# reports = parse_input('test-input')
39+
reports = parse_input('input')
40+
41+
# Part 1
42+
safe = count_safe(reports)
43+
print(f"{safe} reports are safe")

‎2024/day02/test-input

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
7 6 4 2 1
2+
1 2 7 8 9
3+
9 7 6 2 1
4+
1 3 2 4 5
5+
8 6 4 4 1
6+
1 3 6 7 9

0 commit comments

Comments
 (0)
Please sign in to comment.