adventofcode

2025-12-05T05:34:07.626389Z

Day 5 - Solutions

Boldizsár Rácz 2025-12-05T09:29:31.450199Z

Casey 2025-12-05T09:51:40.716499Z

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

jurjanpaul 2025-12-05T10:16:56.575109Z

https://jurjanpaul.github.io/ape-cljs-playground/?code=KGRlZiBpbnB1dAogIDs7IHRvIGJlIHJlcGxhY2VkIHdpdGggcmVhbCBpbnB1dAogICIzLTUKMTAtMTQKMTYtMjAKMTItMTgKCjEKNQo4CjExCjE3CjMyIikKCihkZWZuIHBhcnNlCiAgW2lucHV0XQogIChsZXQgW2xpbmVzCiAgICAgICAgKHN0cmluZy9zcGxpdC1saW5lcyBpbnB1dCldCiAgICB7OmZyZXNoLXJhbmdlcwogICAgICgtPj4gbGluZXMKICAgICAgICAgICh0YWtlLXdoaWxlICMobm90IChzdHJpbmcvYmxhbms%2FICUpKSkKICAgICAgICAgIChtYXAgKGZuIFtsaW5lXQogICAgICAgICAgICAgICAgICgtPj4gKHN0cmluZy9zcGxpdCBsaW5lICMiLSIpCiAgICAgICAgICAgICAgICAgICAgICAobWFwIHBhcnNlLWxvbmcpKSkpKQogICAgIDppZHMKICAgICAoLT4%2BIGxpbmVzCiAgICAgICAgICAoZHJvcC13aGlsZSAjKG5vdCAoc3RyaW5nL2JsYW5rPyAlKSkpCiAgICAgICAgICBuZXh0CiAgICAgICAgICAobWFwIHBhcnNlLWxvbmcpKX0pKQoKKGRlZiBwYXJzZWQgCiAgKHBhcnNlIGlucHV0KSkKCihkZWZuIGxpc3RlZD8KICBbcmFuZ2VzIGlkXQogIChzb21lIChmbiBbW2EgYl1dICg8PSBhIGlkIGIpKQogICAgICAgIHJhbmdlcykpCgooZGVmbiBwYXJ0MSBbXQogIChsZXQgW3s6a2V5cyBbZnJlc2gtcmFuZ2VzIGlkc119IHBhcnNlZF0KICAgICgtPj4gaWRzCiAgICAgICAg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1yYW5nZXMgcGFyc2VkKSkKICAgICAgIChtYXAgKGZuIFtbYSBiXV0gKGluYyAoLSBiIGEpKSkpCiAgICAgICAocmVkdWNlICspKSkKICAKKHByaW50bG4gInBhcnQyOiIgKHBhcnQyKSkK&checksum=MTYzMDg5ODMxMQ%3D%3D

benoit 2025-12-05T11:25:29.705879Z

Merging overlapping ranges. https://github.com/benfle/advent-of-code/blob/main/src/advent_of_code/2025/05.clj

narimiran 2025-12-05T12:18:12.347589Z

Managed to write the notebook earlier than usual: https://narimiran.github.io/aoc2025/src/day05/

2025-12-05T13:57:14.207529Z

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

hlship 2025-12-05T16:53:00.250919Z

This year seems easier than the last time I participated in 2023.

hlship 2025-12-05T16:56:16.214519Z

(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))

narimiran 2025-12-05T17:00:58.985919Z

@hlship if you sort your ranges, you won't need 5 different cases, there are just 2 to consider.

genmeblog 2025-12-05T17:09:07.381239Z

I can say even one.

rjray 2025-12-05T18:03:32.077959Z

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.

narimiran 2025-12-05T18:06:56.636439Z

@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?

☝️ 1
genmeblog 2025-12-05T18:09:38.901039Z

Haha. Indeed. I meant one condition only + max for the right boundary.

2025-12-05T19:51:36.791769Z

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 🙂

2025-12-05T20:07:59.293099Z

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

snyssfx 2025-12-05T20:41:04.615489Z

for part 2 I sorted all the ranges by the left element and then joined them together

Logan Turner 2025-12-05T22:07:52.881639Z

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.

Sam Ferrell 2025-12-06T03:56:10.270369Z

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

2025-12-06T04:57:43.645039Z

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))

👍 1
Logan Turner 2025-12-06T05:49:30.219679Z

@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.)

🙂 1
2025-12-06T19:58:36.397369Z

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.

1