Skip to content

Commit d85e2bb

Browse files
committed
Add day05, 2025
1 parent 1a53da8 commit d85e2bb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

2025/day05/solution.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
with open("input") as f:
2+
inp = f.read().strip()
3+
4+
ranges, ids = inp.split("\n\n")
5+
ranges = [(int(x), int(y)) for x, y in (r.split("-") for r in ranges.split("\n"))]
6+
ids = list(map(int, ids.split("\n")))
7+
8+
9+
# Part 1
10+
def is_fresh(id_):
11+
for x, y in ranges:
12+
if x <= id_ <= y:
13+
return True
14+
return False
15+
16+
17+
print(sum(is_fresh(id_) for id_ in ids))
18+
19+
20+
# Part 2
21+
ranges.sort()
22+
23+
merged = []
24+
cur_x, cur_y = ranges[0]
25+
26+
for x, y in ranges[1:]:
27+
if x <= cur_y + 1:
28+
cur_y = max(cur_y, y)
29+
else:
30+
merged.append((cur_x, cur_y))
31+
cur_x, cur_y = x, y
32+
33+
merged.append((cur_x, cur_y))
34+
print(sum(y - x + 1 for x, y in merged))

0 commit comments

Comments
 (0)