Day 5 - Solutions
https://gitlab.com/maximoburrito/advent2025/-/blob/main/src/day05/main.clj
https://github.com/Ramblurr/advent-of-code/blob/main/src/aoc/2025/day05.clj man I thought I was clever by anticipating what we needed in part 2, but it was a curveball instead
Merging overlapping ranges. https://github.com/benfle/advent-of-code/blob/main/src/advent_of_code/2025/05.clj
Managed to write the notebook earlier than usual: https://narimiran.github.io/aoc2025/src/day05/
https://github.com/tschady/advent-of-code/blob/main/src/aoc/2025/d05.clj
https://github.com/FelipeCortez/advent-of-code/blob/master/2025/05.clj
https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2025/day05.clj
Part 2 tripped me up so much today. Sample passed but input did not. Turned out I had my overlap check backwards 😓 https://github.com/brandoncorrea/advent-of-code/blob/master/clojure/src/aoc/y2025/day05.cljc
This year seems easier than the last time I participated in 2023.
(defn combine-range
[other-ranges [low high]]
(loop [low low
high high
result []
[range & more-ranges] other-ranges]
(if (nil? range)
(conj result [low high])
(let [[olow ohigh] range]
(cond
(<= olow low high ohigh) ; other range consumes our range
(recur olow ohigh result more-ranges)
(<= low olow ohigh high) ; we consume other range
(recur low high result more-ranges)
(<= low olow high) ; other low inside our range
(recur low ohigh result more-ranges) ; combine
(<= low ohigh high) ; other high inside our range
(recur olow high result more-ranges)
:else ; no overlap
(recur low high (conj result range) more-ranges))))))
(defn combine-ranges
[ranges]
(reduce combine-range [] ranges))@hlship if you sort your ranges, you won't need 5 different cases, there are just 2 to consider.
I can say even one.
https://github.com/rjray/advent-2025-clojure/blob/master/src/advent_of_code/day05.clj Was out with friends, so I didn't start until the puzzle had been unlocked for two hours. Part 1 went fine, albeit feeling a little brute-force-ish. My logic for merging ranges in part 2 was wrong, so I got that one wrong on the first try. By this time it was pushing midnight for me so I slept on it. This morning I took a look at Norman's solution, but in the end I went with an algorithm I found on a site called "AlgoCademy" (https://algocademy.com/) that was O(n) after a sort of the ranges.
@tsulej Can't be one, can it? Even you have if (<= a na b) ... in your code, so you're considering two cases, don't you?
Haha. Indeed. I meant one condition only + max for the right boundary.
oh yeah, I could definitely optimize mine. I basically just n^2 check all the ranges and stop if nothing merged. I started doing something more but then realized there was <200 pieces of data so I didn't expect any performance problems. Sometimes I'll do a little cleanup after the fact, but last night I opted for sleep 🙂
My solution, sorting the ranges + merging overlapping ranges in a linear pass https://github.com/ChrisBlom/advent-of-code/blob/master/src/adventofcode/2025/day05.clj#L1
for part 2 I sorted all the ranges by the left element and then joined them together
Did roughly the same sort-then-merge way to combine ranges as everyone else, then stuck it in a sorted-map for O(log(n)) time lookup of IDs.
Spent an embarassing amount of time thinking I could subtract the amount of IDs that are overlapped from the total as it is in the input... then instead went on to do some very funky splitting of overlapped ranges instead of merging... 🫣 https://github.com/samcf/advent-of-code/blob/main/2025-05-cafeteria.clj
Ranges can be compressed without sorting, recursion/looping, and with just two overlap checks (as opposed to 4).
(defn- in-bin? [[start end] ingredient]
(<= start ingredient end))
(defn- bins-overlap? [[start-1 :as bin-1] [start-2 :as bin-2]]
(or (in-bin? bin-1 start-2)
(in-bin? bin-2 start-1)))
(defn- join-bins [bins]
[(apply min (map first bins))
(apply max (map second bins))])
(defn- overlapping-bins [bin bins]
(->> bins
(filter #(bins-overlap? bin %))
set))
(defn- conj-bin [bins bin]
(let [overlapping (overlapping-bins bin bins)]
(conj (set/difference bins overlapping)
(join-bins (conj overlapping bin)))))
(defn- compress-bins [bins]
(reduce conj-bin #{} bins))@bwancor I like how you’ve factored this. The tradeoff of sorting is that it takes an upfront n*log(n) time penalty in order to avoid the n^2 penalty of comparing every range to a growing set of joined ranges. (I think your code ends up comparing each bin to about n/4 other bins on average.)
I had to do something similar with 3D objects a while back (lat/lons + altitude) and used the recursive approach. Looking back now, I probably could have made that code way faster by avoiding recursion.