adventofcode

2025-12-02T05:28:18.477939Z

Day 2 - Solutions

hlship 2025-12-02T05:49:53.432349Z

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.

Aleks 2025-12-02T05:53:09.443809Z

#"^(.+)\1+$" 🀩

❀️ 1
hlship 2025-12-02T06:00:31.887229Z

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

hlship 2025-12-02T06:04:06.116969Z

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.

Sam Ferrell 2025-12-02T06:42:34.947819Z

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.

rjray 2025-12-02T07:07:30.733789Z

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.

Casey 2025-12-02T07:30:33.297929Z

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

hlship 2025-12-03T17:46:23.682549Z

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!

πŸ’― 1
Casey 2025-12-03T18:56:09.692569Z

So true, This is why I love these threads!

jurjanpaul 2025-12-02T10:32:38.345479Z

In Scittle on my phone: https://jurjanpaul.github.io/ape-cljs-playground/?code=KGRlZiBpbnB1dAogIDs7IHRvIGJlIHJlcGxhY2VkIHdpdGggcmVhbCBpbnB1dAogICIxMS0yMiw5NS0xMTUsOTk4LTEwMTIsMTE4ODUxMTg4MC0xMTg4NTExODkwLDIyMjIyMC0yMjIyMjQsC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mFsaWQyPykpKQoKKGRlZm4gcGFydDIgW10KICAoLT4%2BIHBhcnNlZAogICAgICAgKG1hcGNhdCBnYXRoZXItaW52YWxpZHMyKQogICAgICAgKHJlZHVjZSArKSkpCgoocHJpbnRsbiAocGFydDIpKQo%3D&checksum=LTEzMjI1ODgz Fast enough, but can imagine using regexes would indeed be faster.

1
πŸŽ‰ 1
Felipe 2025-12-02T13:19:14.096809Z

slow but clean, clean but slow https://github.com/FelipeCortez/advent-of-code/blob/master/2025/02.clj

Felipe 2025-12-02T13:23:00.618039Z

wow the regex ones are super short. wish I'd thought of that instead

Mateusz Mazurczak 2025-12-02T14:16:09.392369Z

Without regex: https://mateuszmazurczak.com/aoc/2025/2#solution-d8c0374c-b149-4a9f-a75f-3a3236878692

narimiran 2025-12-02T14:31:21.224569Z

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

Mateusz Mazurczak 2025-12-02T14:53:23.805129Z

@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

Mateusz Mazurczak 2025-12-02T15:06:17.632089Z

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

Mateusz Mazurczak 2025-12-02T15:20:29.932319Z

Got it down to 45 ms

Mateusz Mazurczak 2025-12-02T15:26:21.905619Z

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

d._.b 2025-12-02T15:27:10.061909Z

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

;; 34421651192

d._.b 2025-12-02T15:27:23.073239Z

It ain't fast, but it gets the job done.

Maravedis 2025-12-02T16:17:03.150539Z

Regexes really make this trivial. For part1, remove the + after \1.

2025-12-02T16:51:08.326619Z

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

narimiran 2025-12-02T17:16:03.067709Z

My notebook for Day 2: https://narimiran.github.io/aoc2025/src/day02/

πŸ†’ 1
rfisher 2025-12-02T19:04:51.234029Z

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

benoit 2025-12-02T19:34:13.287429Z

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

snyssfx 2025-12-02T20:37:45.440369Z

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:

Andrew Byala 2025-12-02T20:51:43.370449Z

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

2025-12-02T22:02:55.128279Z

sticking with transduce just for fun

Joseph Ferano 2025-12-03T02:47:19.894379Z

Joseph Ferano 2025-12-03T02:47:34.845159Z

Did it the Clojure way cause my goal is to become fluent in Clojure

Joseph Ferano 2025-12-03T02:48:12.507499Z

But the regex is certainly elegant

genmeblog 2025-12-04T17:43:11.910679Z

catching up, brute force, no regex solution: https://github.com/genmeblog/advent-of-code/blob/master/src/advent_of_code_2025/day02.clj

Trevor Cook 2025-12-08T01:43:38.093699Z

Non-regex, non-brute-force, fast but not that pretty.

Gregory Bleiker 2025-12-02T16:24:57.609359Z

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?

Rider Sargent 2025-12-02T16:27:43.649699Z

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.

πŸ‘ 1
Gregory Bleiker 2025-12-02T16:28:59.901109Z

Thanks @rider.sargent, an eye opener :)

Rider Sargent 2025-12-02T16:30:16.892669Z

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. πŸ˜‰