From 424ffd61c58eb127f9a657a854737e2d82368217 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 5 Dec 2025 06:39:31 +0100 Subject: Day 5 2025 --- 2025/05/a.py | 15 +++++++++++++-- 2025/05/b.py | 26 ++++++++++++++++++++++++++ 2025/README.md | 8 ++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 2025/05/b.py (limited to '2025') diff --git a/2025/05/a.py b/2025/05/a.py index 5fdb11b..b2699ba 100644 --- a/2025/05/a.py +++ b/2025/05/a.py @@ -1,6 +1,17 @@ import fileinput +rang = True +ranges = [] +s = 0 with fileinput.input() as lines: for line in lines: - ... - + if line == '\n': + rang = False + elif rang: + i = line.find('-') + ranges.append((int(line[:i]), int(line[i+1:-1]))) + else: + x = int(line[:-1]) + if any(x >= r[0] and x <= r[1] for r in ranges): + s += 1 +print(s) 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 @@ +import fileinput + +def overlap(r1, r2): + return min(r1[1], r2[1]) >= max(r1[0], r2[0]) + +def fuse(r1, r2): + return (min(r1[0], r2[0]), max(r1[1], r2[1])) + +ranges = [] +with fileinput.input() as lines: + for line in lines: + if line == '\n': + break + i = line.find('-') + ranges.append((int(line[:i]), int(line[i+1:-1]))) + +s = set() +for r in ranges: + c = r + for rr in list(s): + if overlap(c, rr): + c = fuse(c, rr) + s.remove(rr) + s.add(c) + +print(sum(r[1] - r[0] + 1 for r in s)) diff --git a/2025/README.md b/2025/README.md index 8fccef4..a77c168 100644 --- a/2025/README.md +++ b/2025/README.md @@ -11,6 +11,7 @@ Example ``` Day -Part 1- -Part 2- + 5 00:04:39 00:22:19 4 00:25:36 00:27:49 3 00:05:02 00:16:09 2 00:10:36 00:11:55 @@ -61,3 +62,10 @@ out of bounds. For part 2 I decided to quickly code the dumb "repeat part 1 until no rolls are removed" strategy and it worked. + +### Day 5: Cafeteria + +Part 2 required a little bit of thinking to handle overlaps correctly +- at first I wrote a solution that did not handle overlaps, then one +that can only handle single overlaps, and finally one that works in +every case. My final solution is quite straightforward. -- cgit v1.3