-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc5.py
More file actions
59 lines (43 loc) · 1.07 KB
/
Copy pathaoc5.py
File metadata and controls
59 lines (43 loc) · 1.07 KB
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
51
52
53
54
55
56
57
58
59
file_path = "data/aoc5.txt"
ex = """3-5
10-14
16-20
12-18
1
5
8
11
17
32"""
def process_text(file: str) -> tuple:
mode_ranges = True
ranges = []
ids = []
for line in file.split("\n"):
if line in ["", " "]:
mode_ranges = False
continue
if mode_ranges:
a, b = line.split("-")
ranges.append([int(a), int(b)])
else:
ids.append(int(line))
return ranges, ids
def find_overlaps(ranges: list) -> list:
merged = []
for r in sorted(ranges):
if not merged or merged[-1][1] < r[0] - 1:
merged.append(r)
else:
merged[-1][1] = max(merged[-1][1], r[1])
return merged
with open(file_path, "r") as file:
ranges, ids = process_text(file.read())
# ranges, ids = process_text(ex)
#################### TASK 1 ####################
res = sum(any(a <= i <= b for a, b in ranges) for i in ids)
print(res)
#################### TASK 2 ####################
merged = find_overlaps(ranges)
res = sum([b - a + 1 for a, b in merged])
print(res)