Advent 2026 Day 5
Tantalisingly close to graph theory.
Part 1
You are given a set of buttons which perform certain calculations:
[A]: append digit[*A]: multiply by value[/A]: divide (truncating down) by value[A->B]: replace digitAwith digitB[oct]: convert to octal representation[bksp]: remove rightmost digit
Removing the rightmost digit when the current display contains only a
single digit resets to 0. Similarly, appending to 0 results in
just the value. We determine all numbers that can be achieved with at
most three button presses.
First, there’s a little bit of parsing to get from the input format to something a bit friendlier. I chose to parse multiply and divide into numbers, but the others are retained as strings: we’ll store the intermediate state as a string and convert to a number as necessary.
(define (parse-button button)
(cond
((string= button "oct")
'(oct))
((string= button "bksp")
'(bksp))
((char=? (string-ref button 0) #\*)
(list 'mult (string->number (string-drop button 1))))
((char=? (string-ref button 0) #\/)
(list 'div (string->number (string-drop button 1))))
(#t
(let ((arrow (string-index button #\-)))
(if arrow
(list 'replace
(substring button 0 arrow)
(substring button (+ arrow 2)))
(list 'append button))))))
We can then split the input and parse the buttons to our internal representation.
(define (parse-input input)
(filter-map
(lambda (button)
(and
(not (string-null? button))
(parse-button button)))
(string-split input (char-set #\[ #\] #\ ))))
And the final bit of machinery is to define how a button press transforms the current state, which just pattern matches over our button definitions. It feels perhaps a bit unsatisfying to perform repeated string-to-integer conversions, but I don’t really see how the replace operation can coexist with the others. Converting to octal is also slightly strange.
(define (press button state)
(match button
(('append n)
(if (string= state "0")
n
(string-append state n)))
(('mult n)
(number->string (* (string->number state) n)))
(('div n)
(number->string (floor (/ (string->number state) n))))
(('replace a b)
(string-replace-substring state a b))
(('oct)
(number->string
(string->number state)
8))
(('bksp)
(if (= (string-length state) 1)
"0"
(string-drop-right state 1)))))
Now we can define the search over all button presses up to three. This is just a recursive procedure, maintaining seen states in a hash map. Making the assumption that no button in the input appears twice, we don’t need to terminate on an already seen state: all button press combinations will be unique.
(define (press-possibilities seen pressed buttons state)
(hash-set! seen state #t)
(when (< pressed 3)
(for-each
(lambda (button)
(press-possibilities seen (+ pressed 1) buttons (press button state)))
buttons))
seen)
Finally, we need to get the sum of digit combinations that we’ve seen. Define a small utility procedure to sum string keys of a hash map as an integer, and run the algorithm.
(define (sum-keys h)
(hash-fold
(lambda (n _ acc) (+ acc (string->number n)))
h))
(define (part1 input)
(sum-keys
(press-possibilities (make-hash-table) 0 (parse-input input) "0")))
Part 2
Now, instead of finding button combinations, we’re finding digit combinations. To provide a termination condition, if any button press would cause the state to contain more than four digits, it does nothing instead. We have to be slightly clever to avoid repeating computations: because we don’t care about which buttons have been pressed, our state space consists only of the digit combinations.
(define (digit-possibilities seen buttons state)
(when (<= (string-length state) 4)
(hash-set! seen state #t)
(for-each
(lambda (button)
(let ((next (press button state)))
(unless (hash-ref seen next)
(digit-possibilities seen buttons next))))
buttons))
seen)
(define (part2 input)
(sum-keys
(digit-possibilities (make-hash-table) (parse-input input) "0")))
Despite never explicitly constructing a graph, we’ve ended up doing depth-first search on implicit graphs.