-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.py
More file actions
23 lines (20 loc) · 760 Bytes
/
Copy pathday05.py
File metadata and controls
23 lines (20 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from bisect import bisect_left
ranges, items = open("inputs/day05.txt").read().split("\n\n")
items = [int(item) for item in items.split("\n")]
ranges = [[int(n) for n in r.split("-")] for r in ranges.split("\n")]
ranges.sort()
# Merge all overlapping ranges.
merged_ranges = []
current = ranges[0]
for i, r in enumerate(ranges):
if r[0] > current[1] + 1:
merged_ranges.append(tuple(current))
current = r
else:
current[1] = max(current[1], r[1])
merged_ranges.append(tuple(current))
def fresh(item):
l, h = merged_ranges[max(bisect_left(merged_ranges, (item, item))-1, 0)]
return l <= item <= h
print(f"a = {sum(1 if fresh(item) else 0 for item in items)}")
print(f"b = {sum(r[1] - r[0] + 1 for r in merged_ranges)}")