On this page:
10.1 What’s the length of the sequence after 40 iterations?
10.2 After 50 iterations?
10.3 Testing Day 10

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))
      (map ~a
           (append-map (λ (digit-run)
                         (list (string-length digit-run)
                               (substring digit-run 0 1)))

(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))