▼ Advent of Code:   solutions & explanations
 1 Day 1 2 Day 2 3 Day 3 4 Day 4 5 Day 5 6 Day 6 7 Day 7 8 Day 8 9 Day 9 10 Day 10 11 Day 11 12 Day 12 13 Day 13 14 Day 14 15 Day 15 16 Day 16 17 Day 17 18 Day 18 19 Day 19 20 Day 20 21 Day 21 22 Day 22 23 Day 23 24 Day 24 25 Day 25 Index
7.3

### 10Day 10

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

The puzzle. Our input is a short numeric key.



#### 10.1What’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.2After 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.3Testing Day 10

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