Advent of Code 2026 Day 2
Table of Contents
1. Advent of Code 2026 Day 2
1.1. Part one
Today's problem was a good bit harder than day one, but not too difficult either. For this problem, you get a sequence of commands corresponding to waypoints. The goal was to calculate the total distance traveled by going to every waypoint in the input in order.
My first thought was to just make a table of waypoint → list of distances to every other waypoint. Sort of like an adjacency map in a graph. That would have probably made my solution a lot simpler, but since I was using Clojure, I thought this was a good problem to play with infinite sequences.
First, the problem says there's an infinite line of repeating waypoints. Clojure can handle this perfectly:
1: (def stops [\r \o \y \g \b \v])
2: (def waypoints (cycle stops))
Now, (take n waypoints) with give me the first \(n\) waypoints for any \(n \ge 0\).
Then, I thought I could use an infinite list of indexes of each different waypoints and use those to construct a sequence of the index of every stop that we are going to. For example, rro would be [0 6 12 13]. We start at position zero, go six waypoints to the next \r waypoint, six more for another \r waypoint, and then just one to get to \b. As a function, this is pretty simple:
- \(f_r(n) = 6n\)
- \(f_o(n) = 6n + 1\)
- \(f_y(n) = 6n + 2\)
and so on.
I wanted something a touch more robust, so I ended up writing a Clojure function that doesn't assume a static order to waypoints:
1: (defn indexes-of [stop points]
2: (keep-indexed #(when (= stop %2) %1) points))
This is a simple function, it just gets the index of every waypoint matching stop in points. I really have an appreciation of how lazy Clojure is–even though points is infinite, indexes-of will give back a lazy sequence that will only be evaluated as far as necessary.
Now, a reduce will let us sort out the indexes that correspond to the list of target waypoints:
1: (defn calc-stop-indexes [stops]
2: (reduce (fn [acc step]
3: (conj acc
4: (first (filter #(> % (last acc))
5: (indexes-of step waypoints)))))
6: [1] stops))
In this code, I thought it was elegant how the filter can be used to drop all the indexes of waypoints we have already passed.
Finally, I just had to go over that sequence and add up all the distances. I did this with another reduce but I could have probably made a cleaner solution using partition or similar.
1: (defn calc-total-distance [stop-indexes]
2: (first (reduce (fn [acc index]
3: (let [total (first acc)
4: prev (second acc)
5: distance (- index prev)]
6: [(+ total distance) index]))
7: [0 1]
8: stop-indexes)))
Then, getting the solution was as simple as (calc-total-distance (calc-stop-indexes commands)).
1.2. Part two
Part two was an interesting twist. Instead of just blindly following the commands, you were supposed to first sort them such that you would go as far as possible. This is where I was glad I hadn't gone with my first solution of an adjacency map since my existing solution required almost no changes to work perfectly for part two. Since the waypoints are in a cyclic order, there is a precedence of waypoints to always go to the next one. Additionally, two of the waypoints are going to be the furthest from one-another. I thought of this sort of like octaves on a piano. So, we can just sort things with the correct precedence and we're basically done. I implemented this pretty simply:
1: (def precedence [ \r \v \b \g \y \o])
2: (def optimal-commands
3: (let [rank (zipmap precedence (range))]
4: (sort (fn [x y] (compare (rank x) (rank y))) commands)))
Then, all of the existing code can be applied to get the correct answer. It's always so satisfying only needing to make a minimal change to part one in order to get a solution to part two.
1.3. Final code
Today's Advent of Code problem was fun, it was in a nice between-zone for me where the code was something I had to tweak to eventually get correct but the problem itself was simple enough that I could see a bunch of different potential ways to go about solving it.
Click to reveal solution
1: (require '[clojure.string :as str])
2:
3: (def commands (seq (str/trim (slurp "input.txt"))))
4: (def stops [\r \o \y \g \b \v])
5:
6: (def waypoints (cycle stops))
7:
8: (defn indexes-of [stop points]
9: (keep-indexed #(when (= stop %2) (inc %1)) points))
10:
11: (defn calc-stop-indexes [stops]
12: (reduce (fn [acc step]
13: (conj acc
14: (first (filter #(> % (last acc))
15: (indexes-of step waypoints)))))
16: [1] stops))
17:
18: (defn calc-total-distance [stop-indexes]
19: (first (reduce (fn [acc index]
20: (let [total (first acc)
21: prev (second acc)
22: distance (- index prev)]
23: [(+ total distance) index]))
24: [0 1]
25: stop-indexes)))
26:
27: (def sol-1 (calc-total-distance (calc-stop-indexes commands)))
28:
29: (def precedence [ \r \v \b \g \y \o])
30: (def optimal-commands
31: (let [rank (zipmap precedence (range))]
32: (sort (fn [x y] (compare (rank x) (rank y))) commands)))
33:
34: (def sol-2 (calc-total-distance (calc-stop-indexes optimal-commands)))