|(require aoc-racket/day10)||package: aoc-racket|
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)))))
We use the same look-and-say function, but with an iteration argument of 50 rather than 40.