aboutsummaryrefslogtreecommitdiff
path: root/2025/05/b.py
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-12-05 06:39:31 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-12-05 06:39:31 +0100
commit424ffd61c58eb127f9a657a854737e2d82368217 (patch)
tree6b3504b264cc9dec2c2f38c7312eb5b766114f2f /2025/05/b.py
parent10caeb18b9c224c8b42ca724e956dba35807ec2c (diff)
downloadaoc-424ffd61c58eb127f9a657a854737e2d82368217.tar.gz
aoc-424ffd61c58eb127f9a657a854737e2d82368217.zip
Day 5 2025
Diffstat (limited to '2025/05/b.py')
-rw-r--r--2025/05/b.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/2025/05/b.py b/2025/05/b.py
new file mode 100644
index 0000000..09bc5e4
--- /dev/null
+++ b/2025/05/b.py
@@ -0,0 +1,26 @@
1import fileinput
2
3def overlap(r1, r2):
4 return min(r1[1], r2[1]) >= max(r1[0], r2[0])
5
6def fuse(r1, r2):
7 return (min(r1[0], r2[0]), max(r1[1], r2[1]))
8
9ranges = []
10with fileinput.input() as lines:
11 for line in lines:
12 if line == '\n':
13 break
14 i = line.find('-')
15 ranges.append((int(line[:i]), int(line[i+1:-1])))
16
17s = set()
18for r in ranges:
19 c = r
20 for rr in list(s):
21 if overlap(c, rr):
22 c = fuse(c, rr)
23 s.remove(rr)
24 s.add(c)
25
26print(sum(r[1] - r[0] + 1 for r in s))

Generated with cgit - Back to sebastiano.tronto.net