Advent of Code 2026 Day 1

Table of Contents

1. Advent of Code 2026 Day 1

1.1. Part one

Part one was pretty simple. Given a list of numbers, find the product of the three most frequent. My initial thought process was the following:

  1. Create a map of values → frequency.
  2. Sort the map by the frequency.
  3. Take the top three and multiply them.

I implemented my solution in Clojure, which was very well-suited to this problem. First, I read the input file as a list of strings:

1: (def lines (str/split-lines (slurp "input.txt")))

Then, generate the frequency map:

1: (def freq-map
2:   (reduce (fn [acc item]
3:             (update acc item (fnil inc 1)))
4:           {} lines))
5: 

The reduce method in Clojure is super handy for this kind of situation; it lets me map over all the items while keeping an accumulator as an argument to each lambda call. fnil is also a really helpful little method that will essentially wrap another function with a default argument. In this case, it creates a function that will either return the result of inc on the value, or 1.

Next, grab the top 3:

1: (def top-3
2:   (->> freq-map
3:        (sort-by val >)
4:        (take 3)
5:        (keys)))

This was another situation where a Clojure method made things really straightforward. sort-by takes a function to get the value to sort by, and a function to perform the sort. In this case I passed val and > to sort in descending order. (take 3) grabs the first three items from the result, and keys gives me a sequence of just the keys (since we don't care about the frequencies anymore).

Finally, get the product:

1: (def sol-1
2:   (apply * (map parse-long top-3)))

I had to use apply here because * expects three arguments, not a sequence which is what top-3 is. apply is sort of like xargs in a shell script. It takes a sequence and passes the items as individual arguments to the function.

Altogether this worked perfectly. Once I got the code running, it got the right solution on the first try, which is always satisfying.

1.2. Part two

When part two was revealed, I couldn't believe my luck. For part two, it was the opposite: the product of the three least frequent items in the list. All I had to do was change the call to sort-by to use < in order to sort by ascending order. I suppose another approach would be to just call reverse right after sort-by, but it's all the same, really.

1.3. Final code

All-in-all, today was a nice and simple problem–it only took me around five minutes to do both parts. Here's the complete solution I wrote for this day.

Click to reveal solution
 1: (require '[clojure.string :as str])
 2: 
 3: (def lines (str/split-lines (slurp "input.txt")))
 4: 
 5: 
 6: (def freq-map
 7:   (reduce (fn [acc item]
 8:             (update acc item (fnil inc 1)))
 9:           {} lines))
10: 
11: (def top-3
12:   (->> freq-map
13:        (sort-by val >)
14:        (take 3)
15:        (keys)))
16: 
17: (def bot-3
18:   (->> freq-map
19:        (sort-by val <)
20:        (take 3)
21:        (keys)))
22: 
23: (def sol-1
24:   (apply * (map parse-long top-3)))
25: 
26: (def sol-2
27:   (apply * (map parse-long bot-3)))