adventofcode 2026-12-01

Good morning. Anyone doing AoC this year?

7

Nice and easy one for warmup, I like it! (And it is not ā€œbefore taking daughter to schoolā€, but ā€œbefore taking son to kindergartenā€! šŸ™‚ ) This year I’ll be doing the notebook format again, here’s day 1: https://narimiran.github.io/aoc2026/day01.html

After solving this in a fairly straightforward way, I wanted to see if I could use https://github.com/cgrand/xforms to go too hard on transducers. So I created this thing that cut my runtime in half:

(require '[net.cgrand.xforms :as xf]
         '[net.cgrand.xforms.io :as xfio])
(xf/some (comp (map parse-long)
               (xf/by-key identity xf/count)
               (xf/sort-by val >)
               (map key)
               (xf/transjuxt {:part-1 (comp (take 3)
                                            (xf/reduce *))
                              :part-2 (comp (xf/take-last 3)
                                            (xf/reduce *))}))
         (xfio/lines-in "day1.txt"))
In the end I think I’m satisfied with the more straightforward solution šŸ˜…

Hm, there seems to be some info missing (or I misunderstood something): The puzzle doesn’t say how to handle ids with equal count

I did not run into this problem

Hm, that is interesting. So your top three IDS were unique by count?

For my real input the frequencies were 7 6 6 5 5 5 …. 3 3 3 2 1 1 so at least I didn’t need to consider that possibility

Certainly a fun warmup for the year. I’m slightly surprised it didn’t need any optimizing. https://github.com/brandoncorrea/advent-of-code/blob/master/clojure/src/aoc/y2026/day01.cljc

> So your top three IDS were unique by count? They don’t have to be unique.

@aaronrebmann as long as the 3rd and 4th place aren’t the same count, the answer is unambiguous. I think the assumption should be that you have all the information and that any theoretical edge case not covered by the instructions can be ignored.

On an unrelated note, I just realized we have clojure.core/frequencies, which is far better than the abomination I wrote this morning.

I’m still not happy with my solution even after using frequencies. I have a very strong feeling I’m missing a key insight here. I feel like my code can be cut down by a lot. I especially dislike the clunky (#(,,,)), and I wonder if I really have to use an if form here with one of the branches being dead. Suggestions (including nitpicks) welcome!

(defn solution2 [s least-common?]
  (->> s
       (#(str/split % #"\n"))
       frequencies
       (sort-by second)
       (#(if least-common?
           %
           (reverse %)))
       (take 3)
       (map (comp Integer/parseInt first))
       (reduce *)))

(defn part1 [s]
  (solution2 s false))

(defn part2 [s]
  (solution2 s true))

sort-by can take another param so you could do (sort-by second >) or (sort-by second <) and pass that as a param https://clojuredocs.org/clojure.core/sort-by

🤯 1
āž• 2

@soltanzadehramin • Since str/split is only your second form, and since your first form is just s, (str/split s #"\n") could be your first form, to avoid the anonymous function. • Clojure’s Integer/parseInt equivalent is parse-long. So you could write (map (comp parse-long first)) (Edit: maybe not truely equivalent, since parseInt is not parseLong) • The reverse of (sort-by f coll) in Clojure can be written as (sort-by (comp - f) coll), and that avoids the overhead of reverse, creating the new seq and looping over it again.

Ok, I gave up and looked at the other solutions šŸ˜„ My approach was certainly a bit overengineered, I think I still have to gain more confidence in doing sequence-y stuff with maps (I always forget that its idiomatic to call second on a map entry) If anyone is interested in my approach and my reflections on where I took a wrong turn, here ya go: https://github.com/Schroedingberg/aoc2026/blob/main/clojure/src/day01.clj

Thanks to @narimiran for the project skeleton and to @bwancor for the reference solution šŸ˜‰

šŸ™‚ 1

Thanks, @bwancor. All great tips. I don’t think it would ever have dawned on me that - also has a unary arity, which of course is obvious in hindsight. I would probably have written #(- 0 %) šŸ˜‚.

I didn’t see your comment before, @thiago.oak. Thanks. I suspected as much, but reading the docs I wasn’t sure what qualifies as a Java comparator.

Checking things like that I suspect is probably something commonly done live in the REPL?

@soltanzadehramin yeah, exploring this live in the REPL (or connecting your editor to a REPL) is really helpful

@aaronrebmann congrats, you’re the first (that I’m aware of) user of the aoc-utils library!

This solves both parts, where in part two pass < as the cmp

(defn solve1
  ([sample]
   (solve1 sample >))
  ([sample cmp]
   (->> sample
        str/split-lines
        (map parse-long)
        frequencies
        (sort-by val cmp)
        (take 3)
        (map key)
        (reduce * 1))))

Ah, I forgot to give an initial value to reduce. I guess I got lucky it worked anyway.

(defn parse-ids
  "Parses `input` into a sequence of numeric IDs."
  [input]
  (->> input
       str/split-lines
       (map parse-long)))

(defn ids-by-frequency
  "Returns the unique IDs in `ids`, from most to least frequent."
  [ids]
  (->> ids
       frequencies
       (sort-by val >)
       (map key)))

(defn part1
  "Returns the product of the three most common IDs in `input`."
  [input]
  (->> input
       parse-ids
       ids-by-frequency
       (take 3)
       (reduce *)))

(defn part2
  "Returns the product of the three least common IDs in `input`."
  [input]
  (->> input
       parse-ids
       ids-by-frequency
       (take-last 3)
       (reduce *)))
Notes: https://github.com/emseidov/aoc/blob/main/2026/clojure/docs/day01.md

@soltanzadehramin you don’t have to pass an init value but it’s good practice. Also, you could use split-lines instead of

(#(if least-common?
           %
           (reverse %)))
You could write
((if least-common? take take-last))
or
((if least-common identity reverse))
Two pairs of parens so the sequence will get threaded last

Hmm… how does that work? Does the macro flatten the forms as the final step, or is this a trick where the if-form is returning the symbol that then becomes invoked in the outer parens?

@soltanzadehramin On the single-arity -, what’s also interesting is that / can be unary, and +, *, and and or can be 0-arity:

(- 2) => -2
(/ 2) => 1/2
(+)   => 0
(*)   => 1
(and) => true
(or)  => nil

@soltanzadehramin the if expression does not return the symbol, it returns the value of the function. The macro is invoked first, flattening the forms, leaving you with

((if cond then else) expression)
Then, and I’m not sure if it’s formalized anywhere, Clojure follows normal order evaluation, so expressions are evaluated left to right The if expression is evaluated. First the condition, then whatever clause is the consequence, this returns a value, in our case a function. Then the expression to the right is evaluated Then the function call is evaluated (whatever the function may be)

Wow. I didn't realize a returned function could be invoked directly. Not sure why, probably confused the two separate concepts of first list item being invoked and macroexpansion.

Valentin De Negri Fischer 2026-12-02T14:15:32.034649Z

;; Part 1
(->> i/input
     (frequencies)
     (sort-by second >)
     (take 3)
     (reduce (fn [acc [val _]]
               (* acc val)) 1))
;; Part 2
(->> i/input
     (frequencies)
     (sort-by second)
     (take 3)
     (reduce (fn [acc [val _]]
                (* acc val)) 1))

šŸŽ‰ 1

Clojure's destructuring capabilities are pretty cool as well.

Meowdy friends! šŸ‘‹ Advent of Code officially starts today and on behalf of the clojure team I’ve prepared a special gift šŸ’ ✨ Introducing: Power Tools & Parens šŸ› ļø - https://lambduhh.github.io/powertools-and-parens/?campaign=aoc26&ref=slack-aoc • aoc-star announcement blog/RFC aka https://lambduhh.github.io/powertools-and-parens/blog/announcement.html What’s included you may ask?: • https://github.com/lambduhh/clojure-template-aoc - the clojure starter template you’ve been yearning for. preloaded with libs and how to guides • https://lambduhh.github.io/powertools-and-parens/learn/(parsing, graphs, combinatorics… read the ā€œwhy i built thisā€ to find out how I knew exactly what those were. • Snazzy no install options: ā—¦ A preloaded https://lambduhh.github.io/powertools-and-parens/workbench/ with exercises to try things in the browser (uses scittle - thanks @borkdude) ā—¦ Instructions to run template using codespaces in README • Spoiler-safe: solutions wait until 3 days after each puzzle unlocks. … but wait there’s more! One of the whole points of this is to connect with people so anticipate some fun! • Mob sessions! • Pairing sessions! • Collaborations galore! 🌈 So get ready for all the fun sessions with ya girl Lambduhh + favorite clojure peeps. 😜 Content starts going up by end of this week! v::skin-tone-3 šŸ šŸ“˜

šŸ‘ 4
šŸ‘šŸ½ 1

I’m curious to read people’s solutions. From a beginner’s perspective it would also be fun to have others review my solution and suggest simplifications. Is there a place to do this type of thing without spoiling it for those who haven’t yet attempted their own solutions? Perhaps a daily thread here with spoiler warnings?

hey Ramin - I’d love to make that happen! Would you be open tohttps://lambduhh.github.io/powertools-and-parens/blog/session-formats.html with a few other folks to discuss your day 1 solution? I was reviewing the https://clojurians.slack.com/archives/C0GLTDB2T/p1796103469938319above and thinking it would be great if we could continue the conversation with @vincent.cantin @aaronrebmann @odd.andreas @narimiran

šŸŽ‰ 1

Of course there was such a thread already. I guess I am blind! What does joining a session to discuss the solution entail?

joining a quick video chat (30 min; within next 48 hrs, time TBD depending on other people’s availability).

šŸ‘ 1

if we can get 3 people to join the session then each person would have 5 min to ā€œshow and tellā€ their solution, followed by 5 min of open dialogue/discussion/review

But for Day 1 there isn’t much to discuss, is it?

well that depends. I think there is always an opportunity for dialogue as long as there is someone asking good questions and sure.. i suppose it could very well be that each person shares their solution, each solution is exactly the same, and each person got to it in exactly the same way using the same exact workflow/tooling/IDE and in that case i guess it would just be a short session šŸ™ƒ

That sounds like fun, but I think I will unfortunately have to decline the offer. I don’t really know when I will be available.

@lorilynjmiller IMO, wait until Day 3. By that time, there should be different approaches for some task (for Day 1 there was basically just one), and even if there aren’t, you now have three different tasks to discuss.

šŸ’œ 1

@lorilynjmiller I’m open to chat about the code, no problem.

šŸ’œ 1

awesome! so i’ll share a bit more context… (here’s full https://lambduhh.github.io/powertools-and-parens/blog/announcement.html) this year, for each day of advent of code the clojure team (i.e. me lol) will release some form of content. on https://lambduhh.github.io/powertools-and-parens/ (there will be a 3-5 day lag to prevent spoilers and allow for post production) formathttps://lambduhh.github.io/powertools-and-parens/of sessions and content released will vary based on availability and the actual challenges released… since there was some interest in this thread I figured I’d shoot my shot and make my pitch šŸ˜… but ngl I do agree that the first few days may not lend themselves to in depth discussion…

the whole thing is a pretty dynamic experiment tbh šŸ˜… just gotta go w the flow and hopefully get to talk about some code

Im totally on board

šŸ™ŒšŸ¼ 1

I am in UTC+1 - something like the clojure camp scheduling app would probably help us a lot šŸ˜…

šŸ’Æ 1
šŸ˜‚ 1

Otherwise I am quite flexible at the moment

UTC+2 here šŸ‡«šŸ‡® ā˜ƒļø, available most of the time.

@lorilynjmiller Oooh, so that’s (semi?) official? I’ve never done such video-thingy and English is not my first language, but - I might be interested in discussing my solutions and different approaches šŸ™‚ (Even though I already do both in written form in https://narimiran.github.io/aoc2026/)

awesome @narimiran! I think I have the day1 gameplan already set now with @vincent.cantin and @aaronrebmann but since you are an experienced veteran I’d love to get with you on some of next week’s challenges! ^~^

šŸ‘ 1

When i see solutions in other languages, I always feel like I cheated using Clojure. clojure (I just couldn’t be bothered to write that much more code.)

āž• 2

Yeah, Clojure is nice for AoC, but so is Python.

E.g. for Day 1, there is Counter, and Python also has what Clojure doesn’t: a way to get last elements with destructuring: a, b, c, *_, x, y, z = my_sorted_counter .

hhm …. Clojure still is not so bad in this aspect. Let’s say that Clojure has it but in a minimalist way. (let [[z y x] (rseq-or-reverse my-sorted-counter)] ,,,)

ok, it does not quite have it, but with macro we can add it.

REPL driven development fits so naturally with AoC. Its like you're vibe checking your code as you think through parsing, algos ... oh wait.. :-/