aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit 424ffd61c58eb127f9a657a854737e2d82368217
parent 10caeb18b9c224c8b42ca724e956dba35807ec2c
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Fri,  5 Dec 2025 06:39:31 +0100

Day 5 2025

Diffstat:
M2025/05/a.py | 15+++++++++++++--
A2025/05/b.py | 26++++++++++++++++++++++++++
M2025/README.md | 8++++++++
MREADME.md | 2+-
4 files changed, 48 insertions(+), 3 deletions(-)

diff --git 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 @@ -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 @@ -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. diff --git a/README.md b/README.md @@ -14,4 +14,4 @@ See `year/README.md` for instructions on how to run my code. |2022| 50 | Rust | Done in 2025 to learn Rust | |2023| 50 | C | All solved by December 25, 2023 | |2024| 50 | C++ | Each solved within 24h | -|2025| 8 | Python | Work in progress... | +|2025| 10 | Python | Work in progress... |