Learning Lisp with Advent of Code '26, Day 2
I have long been afraid of learning Lisp, with all of its lovely parentheses haunting me menacingly, and since I would call myself a relative novice when it comes to functional programming in general, it was extra scary. However, this semester, betwixt my time spent on classes and research, I have been reading Practical Common Lisp, which has been fun! Now that Advent of Code 2026 is finally here, I am excited to try my hand at using Lisp for fun, contrived jaunts with Santa’s elves. This problem ended up being fun both as a thought exercise and to learn some Lisp, so I thought I’d share my progress on both fronts here. Without further ado, let’s begin!
Elven Farming Robots
Feel free to check the website out for the full problem specification, but the general idea here is that we have a list of waypoints given by the letters in roygbv (I wonder what happened to indigo?), and they are ordered in that way, repeating (for our purposes) endlessly, like roygbvroygbvroygbvroygbvroygbvroygbv.... The robot has a program that is a not-necessarily-as-nicely-ordered sequence of the aforementioned waypoint indicators, and our goal is to figure out how far it traveled. For example, obbvb travels $16$, as in the example from the problem statement.
Problem Pondering
I almost immediately had what I think is an elegant idea, although I’ve yet to check the Reddit for better ones. That being said, what really stuck out here was the modulus of 6, and I got to thinking about some ways we could manipulate that to track which waypoint we were at in a convenient manner. Something to notice here is that the cost of each letter when we start is as follows:
| r | o | y | g | b | v |
|---|---|---|---|---|---|
| 6 | 1 | 2 | 3 | 4 | 5 |
And naturally, once we actually move to another spot, we can think of the costs as shifting by the distance we traveled. For example, in program obbvb, moving to o is distance 1, so the new mapping should look like:
| r | o | y | g | b | v |
|---|---|---|---|---|---|
| 5 | 6 | 1 | 2 | 3 | 4 |
If we then query b, we see that it accurately says it is 3 away from o.
Implementation Ideas (I make a ring buffer)
Now, if we had a magical data structure that allowed for both shifting and $O(1)$ random access, that would be epic!
So we’re basically going to just make a ring buffer, by using a regular vector paired with an integer that stores the index we currently consider the 0 index. This will functionally allow us to shift our starting point as we wish, and when we access, all we need to do is justify our regular $i$-th index access with our 0-index value and a modulus, and we should be good.
More formally, if we define some index map $I$ which stores where each character should point to in the ring buffer:
| r | o | y | g | b | v |
|---|---|---|---|---|---|
| 0 | 1 | 2 | 3 | 4 | 5 |
And we also have the constant vector $C = (6, 1, 2, 3, 4, 5)$ with its companion $ind = 0$ to complete the ring buffer, our recurrence for $ind$ becomes $$ ind = C((I(ch) - ind) \mod 6) $$
Stepping through obbvb
$I(ch)$ takes in the incoming character, say o, and converts it to its translated value 1. Our index is 0, so the subtraction is negligible for now, then we take $C(1) = 1$, so we now set $ind = 1$, and continue!
Next, we have $ind = 1$ and ch = b. Here, $I(b) - 1 = 4-1=3$ and $C(3)=3$, so our $ind = 1+3=4$.
Doing b again, we have $ind = 4$, and here we see $C(4-4 = 0) = 6$. This is a fun step, because we see that our system correctly tracks that the same letter is 6 away from its counterpart.
For v, we have $ind = 10$, and we have $C((I(v) - 10) \mod 6) = C((5 - 10) \mod 6) = C(1) = 1$. Here, notice the modulo of a negative number. Recall that a remainder is the smallest positive integer that should be subtracted from some $a$ to make it divisible by $b$. For $-7 \mod 5$, we find the largest number $\le -7$, which is $-10$, and then subtract $-7$ and $-10$, giving 3. More examples here.
Finishing off the sequence, we have b again, which correctly (Exercise!) gives us $ind=16$ as a result, which is exactly the distance we traveled!
Code Complications
While loading in the data (one long line that I wanted to be treated as chars), I learned of coerce:
(defparameter *input-list* NIL)
(defun load-data (filename)
(with-open-file (in filename)
(setf *input-list*
(coerce (read-line in NIL) 'list))))
coerce will try to force the type through, and here it coerces from a string to a list of characters. The documentation has several other examples, but I found this to be a pretty cool technique! It reminds me a lot of collect() in Rust for objects with the IntoIter trait (hopefully I’m not misremembering).
Note here that all variable names wrapped with * are considered global variables, not necessarily by the compiler but it’s a standard naming convention.
From here, we can’t instantiate a table via constants in Lisp, so we first create a list (the hall mark of the LISt Programming language) and then convert that list into a hash-table instead.
(defparameter *indlist* '((#\r . 0)
(#\o . 1)
(#\y . 2)
(#\g . 3)
(#\b . 4)
(#\v . 5)))
(defparameter *I* ;; indmap
(let ((table (make-hash-table :test 'equal)))
(loop for pair in *indlist* do
(setf (gethash (car pair) table) (cdr pair)))
table))
First, we populate *indlist* with conses (for our purposes, pairs) of chars (denoted by \#<char>) and their corresponding indices. Then, introducing a let binding, we make table and use the key value pairs from the conses to add to our table. setf is a magical thing, and looking at the car/cdr documentation, I saw so many variations…
; Accessors
CAR, CDR, CAAR, CADR, CDAR, CDDR, CAAAR, CAADR, CADAR, CADDR,
CDAAR, CDADR, CDDAR, CDDDR, CAAAAR, CAAADR, CAADAR, CAADDR, CADAAR,
CADADR, CADDAR, CADDDR, CDAAAR, CDAADR, CDADAR, CDADDR, CDDAAR,
CDDADR, CDDDAR, CDDDDR
why are there so many?? I think getting good with these versions will be a fun challenge as I try to get better at Lisp overall.
Next, we have the character map:
(defparameter *C* (vector 6 1 2 3 4 5)) ;; charmap
And finally, the meat of the work for part 1!
(defun solve1 (in-list)
(let ((ind 0))
(loop for ch in in-list do
(incf ind
(aref *C* (mod (- (gethash ch *I*) ind) 6))))
ind))
It’s pretty much exactly what we discussed, incf ind as the ind += and aref for indexing into Lisp vectors being the only major new things here.
I guess also of import is that let bindings have the form:
(let ((var1 init-form-1)
(var2 init-form-2)
...
(varm init-form-m))
declaration1
declaration2
...
declarationp
form1
form2
...
formn)
And as such, we use the first form to loop and increment ind, and the last form to return ind directly!
Part 2??
The second half of this question asks us to, in effect, sort the original program and see if we can induce a maximized distance that way. To this end, my second solution simply inputs a sorted list into solve1:
(defun solve2 (in-list)
(solve1
(sort in-list #'(lambda (x y)
(> (aref *C* (gethash x *I*))
(aref *C* (gethash y *I*)))))))
The most notable part here is just the construction of the lambda allowing us to properly compare our characters – we need to access the costs associated with each character from *C*, and then all is well.
The end
Thanks for reading! Maybe I’ll cook up some other stuff as I slowly get through AoC ’26; learning a new language is tough.
– Arjun