..

Advent 2026 Day 2

A little shift from implementation to algorithm. Definitely felt a little more comfortable not wranging data structures around.

Part 1

You’re given a string of waypoint colours as the puzzle input. Starting on a red waypoint, and assuming an infinite sequence red (r), orange (o), yellow (y), green (g), blue (b), travel along the sequence according to the waypoints. Calculate the total distance.

Start by defining a hash table to map from a colour character to a numeric position. Then the step function gives the distance between any two waypoints. Both these are pretty straightforward.

(use-modules (ice-9 hash-table)
             (ice-9 rdelim))

(define colours
  (alist->hash-table
   (map cons (string->list "roygbv") (iota 6))))

(define (step from to)
  (let ((from (hash-ref colours from))
        (to (hash-ref colours to)))
    (if (<= to from)
        (- (+ 6 to) from)
        (- to from))))

Then we just define a loop over the instructions to accumulate the distance according to step. I feel like I would’ve liked to reach for a more map-reduce style operation: map the step distance over overlapping pairs of elements, and reduce with a sum. This is a pretty natural operation in an array-based language like Uiua or one with a richer sequence library like Factor. Regardless, the loop approach is fine, if slightly more explicit.

(define (distance instructions)
  (let loop ((rest (string->list instructions))
             (pos #\r)
             (len 0))
    (if (null? rest) len
        (loop (cdr rest)
              (car rest)
              (+ len (step pos (car rest)))))))

(define input
  (call-with-input-file "../input/day2" read-line))

(define (part1 input)
  (distance input))

Part 2

We’re given the freedom to rearrange the input sequence. This is an optimisation question, to get the maximum possible distance. I used a greedy observation: from red, the furthest step is to red (6), then violet (5). So we just sort in the order rvbgyo and compute the distance over the new instructions.

(define (reorder-key c)
  (let ((x (hash-ref colours c)))
    (if (zero? x) x
        (- 6 x))))

(define (reorder instructions)
  (list->string
   (sort
    (string->list instructions)
    (lambda (a b) (< (reorder-key a) (reorder-key b))))))

(define (part2 input)
  (distance (reorder input)))

Optimality

We can prove the optimality of this approach. Let’s exclude the trivial case where every waypoint is red. For waypoints a, b and c, dist([a,b,c]) = step(a,b) + step(b,c). By the definition of step, we cancel the intermediate nodes, so we have: dist([a,b,c]) = c - a + 6*n, where n is the number of instruction pairs (a,b) satisfying b <= a. Because c - a is at most 5, we want to optimise for the number of “non-increasing” pairs.

Since we start at red, we must take at least one increasing step to a non-red colour. We have one final tie break: to finish on a red or non-red waypoint. By the argument above, every red -> red step is size 6, so it’s optimal to take them all first.