Day 1 š§µ
That was a good warm up, before taking daughter to school. https://github.com/green-coder/advent-of-code/blob/master/src/aoc_2026/day_1.clj
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?
I did not check
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
@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 š
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 thereās https://clojuredocs.org/clojure.core/comparator if you need it
@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 lastHmm⦠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.
;; 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))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 š š
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
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).
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.
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
I am in UTC+1 - something like the clojure camp scheduling app would probably help us a lot š
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! ^~^
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.)
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.. :-/