r/Clojure • • 3d ago

Looking for feedback on the first Advent of Code puzzle

Hi, this year I'm doing advent of code in Clojure, because the language looked interesting. I'm completely new to it, but I have done some Haskell before.

Here's my repo https://github.com/LelouBil/advent-of-code-2026

I am not sure if the code can be reduced/simplified further, or if the scaffolding in main (to run different days easily) could be improved.

12 Upvotes

12 comments sorted by

5

u/camdez 3d ago

Here are just a few quick notes to get you started:

  1. Underscores are unconventional in Clojure identifiers. They may come up when interoping in some capacity (like destructuring JSON that is named that way), but no one uses them unless practically forced to. :)
  2. It's odd to immediately reach for defrecord. You almost definitely shouldn't use it during AoC problems if you want to learn the Clojure way. Use a map, maaaaybe use a record later for something performance-sensitive.
  3. When writing a multiline function, put a newline after the argument list.
  4. It's fairly unusual to see the get function used. (get coll 0) is (first coll). (get coll 1) is (second coll). (get coll 42) is (nth coll 42).

2

u/LelouBil 3d ago

Thanks for the explanations !

I reached for defrecord because of Haskell x) Usually the first thing I did is to define a type to represent the input properly, and then a function to parse from the text to the type. Clojure seems to work more dynamically typed than I thought.

I noted your input about the other points. I didn't know about first second and nth, and I didn't think to look it up since I wanted to access map entries, but are you telling me they also work for maps ? Are maps and sequences the same under the hood ?

3

u/camdez 3d ago

Well, first off, there's absolutely nothing mechanically wrong with the record code you wrote. But if you're interested in exploring what lessons Clojure has to teach you (or, at least, to challenge you with), you'll want to avoid jumping straight to that record for data modeling.

One of the most foundational ideas in Clojure is that we can get a tremendous amount of mileage out of generic data structures (maps, vectors, lists, etc.)—or, as Alan Perlis famously said: "It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures."

(Clojure gives you those 100 functions.)

I won't try to sell you on the idea (I think you mostly have to experience it yourself), but there's a spectrum from modeling things with very specific types such that they can only participate in interactions that know about those types vs. modeling with generic types that your existing tools can already deal with (printing, sorting, serializing, etc.). Clojure leans hard into the latter approach. (If I'm not being very clear, there's a lot more detail in what I wrote here.)

defrecord is honestly somewhere in the middle of the spectrum—it's a Clojure-y way to define a type and it maintains some of the conveniences of maps—but most of us only reach for it when we have a specific reason to do so, not simply because we have some data.

On the map entry topic... I was mostly reacting to surface features without actually knowing the input shape (I haven't actually seen the AoC problem yet), so I may have gotten some small details wrong, but I can tell you that how that was written was atypical.

Yes, first (and friends) will work on a map. Treating a map like a sequence will cause it to be implicitly converted to a sequence. Note that the order of the map entries is not guaranteed, even though you may notice it appears consistent in practice—don't rely on this.

;; Treating a map like a sequence works just fine
(first {:a 1})
;; =>
[:a 1]

;; Each element of the sequence is a MapEntry
(type (first {:a 1}))
;; =>
clojure.lang.MapEntry

;; `first` works great on a MapEntry
(first (first {:a 1}))
;; =>
:a

;; We can even use other sequence functions...
(count (first {:a 1}))
;; =>
2

Notice what we did here... we took core functions we knew about (e.g. first, count) and got bonus mileage out of applying them in multiple places. It's a small example, but maybe gives a taste of the flavor.

4

u/Krackor 3d ago

It's worth saying that treating a map like a sequable collection is the wrong approach something like 7 out of 10 times. If you want to access data at a particular key you should use the associative lookup (get and friends). If you want to transform the whole map, pair by pair, seqing over the kv pairs is usually an inefficient way to do it and you should reach for higher level tools (merge, reduce-kv, update-vals, etc). Just because you can treat maps as sequences doesn't mean you should or do so often.

1

u/camdez 3d ago

No disagreement that those are the superior tools, but I wouldn't be upset to see a beginner using reduce (over reduce-kv) or map + into to transform keys / values. Heck, we've only had update-keys and update-vals since 1.11 (2022).

2

u/Krackor 3d ago

Your use of get in count-freqs is pretty reasonable. Get is nice to use when you need a default value for the not found case.You may also find fnil particularly useful too.

If you don't need the default value logic, it's common to use the map in function position, or use the key in function position (assuming the key is a keyword). There's nothing particularly wrong about using get for associative lookup, and in many cases it's the clearest option.

2

u/1234pixel 3d ago

Some useful core functions as well as frequencies you might like to add to your toolkit of you've not seen them:

  • second (for sort-by)
  • take-last (for your inverse)
  • apply (an alternative for that reduce)

Sorry about the bad mobile formatting!

2

u/LelouBil 3d ago

Thanks, this is literally my first Clojure program, so It's everything I found online researching during the day.

2

u/quotade 3d ago

This reminds me so much of myself six years ago when I started my Clojure journey--also with AoC (with 20yoe in Java back then). Like writing it as a executable application (instead of evaluating in the editor via a REPL), using "get" to access data in sequences/etc. (instead of first etc. or using the data structure as access function itself), writing functions for stuff the core namespace already has functions for (I for one used a lot of loop+recur before realizing most of that are simple filter/map/reduce operations).

Absolutely try to solve everything on your own, as good as you can, then take a look at a few of the other Clojure solutions and read the docs for all functions you haven't learned yet (I guess you already know https://clojuredocs.org ). You've already used for example map and reduce (where I wrote them as loops back in my first AoC season), so great job! You've already used for example map and reduce (where I wrote them as loops back in my first AoC season), so great job! Shows you're coming from Haskell.

Others already gave a couple of tips. What helped me most was when working with the REPL clicked. You writing a "main" program suggests you're not there yet. Read up on how to integrate the Clojure REPL in your editor. There should be plenty resources on the web. Apart from that and I'm repeating myself now, inspect other repos and learn from their approach in general and the functions they use in particular. (You can find them in the solution mega threads in the AoC subreddit and sometimes also in the Clojure subreddit.)

1

u/LelouBil 1d ago

I actually worked backwards when I realised this. Now my main is just definitions that I import in my day file, and I run my day file in the repl. I'm using the Cursive plugin in IntelliJ

0

u/demosthenex 3d ago

Day one was 2 one-liners. Look at the standard frequencies function, sort and take.

3

u/LelouBil 3d ago

Oh thanks, I didn't even think to search for a built-in frequencies function !