-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
executable file
·28 lines (24 loc) · 851 Bytes
/
5.py
File metadata and controls
executable file
·28 lines (24 loc) · 851 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
#!/usr/bin/env python3
# https://adventofcode.com/2025/day/5
def _1(ranges, ids):
all = set()
for r in ranges:
for id in ids:
if r[0] <= id <= r[1]:
all.add(id)
return len(all)
def _2(ranges):
sr = sorted(ranges, key=lambda x: x[0])
ret, p = [sr.pop(0)], 0
for r in sr:
if ret[p][0] <= r[0] <= ret[p][1]:
ret[p][1] = max(ret[p][1], r[1])
else:
ret.append(r)
p += 1
return sum([(n[1] - n[0] + 1) for n in ret])
if __name__ == "__main__":
# inp = open("input/5_sample.txt", "r").read().split('\n\n')
inp = open("input/5.txt", "r").read().split('\n\n')
ranges, ids = [list(int(x) for x in i.split('-')) for i in inp[0].split('\n')], [int(id) for id in inp[1].split('\n')]
print("1:", _1(ranges, ids), "2:", _2(ranges))