On this page:
<day10>
10.1 What’s the length of the sequence after 40 iterations?
<day10-setup>
<day10-q1>
10.2 After 50 iterations?
<day10-q2>
10.3 Testing Day 10
<day10-test>
8.12

10 Day 10🔗ℹ

 (require aoc-racket/day10) package: aoc-racket

The puzzle. Our input is a short numeric key.

10.1 What’s the length of the sequence after 40 iterations?🔗ℹ

The puzzle asks us to compute the look and say sequence invented by mathematician John Conway. Each iteration of the sequence is the description of the last step if you said it in numbers. So 1 becomes “one 1”, written 11; 11 becomes “two ones”, or 21, then 1211, 111221, and so on.

As in Day 1, this puzzle relies on cumulative state, so we’ll loop using for/fold. To generate the new string for each pass of the loop, we’ll use regexp-match* to find every contiguous run of digits. Each digit run is converted into a list with the number of digits and the digit itself. Then all these lists are concatenated into a new string, and the loop repeats.

The second part of the puzzle is just going to change the number of iterations. So we’ll make one function that can be used for both parts.

(require racket rackunit)
(provide (all-defined-out))
 
(define (look-and-say iterations input-key)
   (for/fold ([start input-key])
             ([i (in-range iterations)])
     (define digit-runs (regexp-match* #px"(\\d)\\1*" start))
     (string-append*
      (map ~a
           (append-map (λ (digit-run)
                         (list (string-length digit-run)
                               (substring digit-run 0 1)))
                       digit-runs)))))

(define (q1 input-key)
  (string-length (look-and-say 40 input-key)))

10.2 After 50 iterations?🔗ℹ

We use the same look-and-say function, but with an iteration argument of 50 rather than 40.

(define (q2 input-key)
  (string-length (look-and-say 50 input-key)))

10.3 Testing Day 10🔗ℹ

(module+ test
  (define input-key (file->string "day10-input.txt"))
  (check-equal? (q1 input-key) 492982)
  (check-equal? (q2 input-key) 6989950))