Day 2 - Solutions
https://gitlab.com/maximoburrito/advent2025/-/blob/main/src/day02/main.clj
This one was easy, though I did panic when I ran my test data through stage 2 code, not stage 1 code, and got a failure. I haven't looked for a more efficient implementation than a lot of string comparisons.
#"^(.+)\1+$" π€©
Funny, a few months ago I enhanced org.clj-commons/pretty to identify repeating sequences of stack frames embedded in a stack trace; largely the same logic (though for pretty, I wanted to find the longest repeating sub-sequence).
Clojure really is pretty ideal for these kinds of exercises; you can leverage lazy evaluation (including concat and mapcat to essentially transform inputs and navigate the results and its more than efficient enough.
https://github.com/samcf/advent-of-code/blob/main/2025-02-gift-shop.clj Still thinking about this... maybe its cheaper to generate invalid IDs from the bottom up than check all the integers in the ranges.
https://github.com/rjray/advent-2025-clojure/blob/master/src/advent_of_code/day02.clj Both were solvable through regexp. I credit my quick solutions more to Perl than Clojure π. I did (however) start about 1:28:00 late due to returning from a hockey game.
I see I'm not the only one using regex for the heavy lifting https://github.com/Ramblurr/advent-of-code/blob/main/src/aoc/2025/day02.clj
It's funny that different problems are easy or hard based on things you may or may not know. I didn't even think of an RE because I forgot about references ... I never use REs that way. Meanwhile, people who didn't think about mod found day 1 hard!
So true, This is why I love these threads!
regex. https://github.com/tschady/advent-of-code/blob/main/src/aoc/2025/d02.clj
slow but clean, clean but slow https://github.com/FelipeCortez/advent-of-code/blob/master/2025/02.clj
wow the regex ones are super short. wish I'd thought of that instead
Without regex: https://mateuszmazurczak.com/aoc/2025/2#solution-d8c0374c-b149-4a9f-a75f-3a3236878692
@mateusz.mazurczak.dev when I tried my code with and without regex, part-1 was faster without it (cc @jurjanpaul502), and part-2 with regex. How it looks like on your side?
@narimiran yeah, pretty much the same: === Part 1 Performance Comparison === No regex: 0.78 ms Regex approach: 284.97 ms === Part 2 Performance Comparison === No regex : 3331.41 ms Regex approach: 334.72 ms
But I didn't optimize it. Like I did for part1, as I was like welp, fuck it π. After optimizing it... it is now faster For part 2 No regex 118 ms Regex 330
Got it down to 45 ms
Basically I optimized it by doing it in a similar fashion as I did part1. https://mateuszmazurczak.com/aoc/2025/2#solution-bcf455ac-af82-4f14-abcc-1baada1f53aa
;; Day 2
; Part I
(defn twice-repeated? [s]
(apply = (split-at (/ (count s) 2) s)))
(->>
(for [ids (str/split (slurp "input-day-2") #",")
:let [[start end] (map parse-long (re-seq #"\d+" ids))
rng (map str (range start (inc end)))
even-strs (filter #(even? (count %)) rng)]
even-str even-strs
:when (twice-repeated? even-str)]
(parse-long even-str))
(reduce +))
;; 19386344315
; Part II
(defn repetition? [s]
(let [inner-string (subs (str s s)
1
(dec (* 2 (count s))))]
(str/includes? inner-string s)))
(->>
(for [ids (str/split (slurp "input-day-2") #",")
:let [[start end] (map parse-long (re-seq #"\d+" ids))
strs (map str (range start (inc end)))]
s strs
:when (repetition? s)]
(parse-long s))
(reduce +))
;; 34421651192It ain't fast, but it gets the job done.
Regexes really make this trivial. For part1, remove the + after \1.
My solution for today, before having seen the other solutions. I need to get better at regex! https://github.com/brandoncorrea/advent-of-code/blob/master/clojure/src/aoc/y2025/day02.cljc
My notebook for Day 2: https://narimiran.github.io/aoc2025/src/day02/
I don't normally like regexes, but I didn't have much time:
(require '[clojure.string :as str])
(defn repeated-numbers [rx n]
(if-let [ret (re-matches rx (str n))] (last ret) nil))
(defn invalids-in-range [rx [a b]]
(filter (partial repeated-numbers rx) (range a (inc b))))
(defn process-range [rx r]
(invalids-in-range rx (map parse-long (str/split r #"-"))))
(defn process-input [rx input]
(reduce + (mapcat (partial process-range rx) (str/split input #","))))
(def input (str/trim (slurp "input-2025-02")))
(println "part 1: " (process-input #"^(\d+)(\1)$" input))
(println "part 2: " (process-input #"^(\d+)\1+$" input))My first version was filtering through all IDs (without regexs). It was slow. I tried to generate the invalid IDs instead. https://github.com/benfle/advent-of-code/blob/main/src/advent_of_code/2025/02.clj
The second part was a bit slow (around 5 secs), I tried to optimize it with a transducer but it didn't help:melting_face:
My day 2 solution. It's nothing fancy, but I think it's pretty clean. β’ https://github.com/abyala/advent-2025-clojure/blob/main/docs/day02.md β’ https://github.com/abyala/advent-2025-clojure/blob/main/src/advent_2025_clojure/day02.clj
sticking with transduce just for fun
Did it the Clojure way cause my goal is to become fluent in Clojure
But the regex is certainly elegant
catching up, brute force, no regex solution: https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2025/day02.clj
Non-regex, non-brute-force, fast but not that pretty.
I'm having a real hard time with the instructions for day 2: For one, the samples seem off:
11-22 has two invalid IDs, 11 and 22.
95-115 has one invalid ID, 99.
998-1012 has one invalid ID, 1010.
...
for line 2, 99 is not even in the ID, same with line 3.
Second, there are cases where two invalid IDs are in a string: 2312312388 has 231231 and 123123 in it. Should both be counted?
Did I miss something or am I overthinking it?The first elements of each line are a range of IDs. So for the second line, the range is from 95 to 115. So 99 is the only ID in that range that is invalid.
Thanks @rider.sargent, an eye opener :)
For part one, this is the relevant piece of instruction: > you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice The word βonlyβ carrying a lot of weight there. π