mbutterick
/
aoc-racket
Archived
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

#### 67 lines 2.5 KiB Racket Raw Normal View History Unescape Escape

 9 years ago #lang scribble/lp2 @(require scribble/manual aoc-racket/helper) @aoc-title[10] @defmodule[aoc-racket/day10] @link["http://adventofcode.com/day/10"]{The puzzle}. Our @link-rp["day10-input.txt"]{input} is a short numeric key. @chunk[ ] 9 years ago @isection{What's the length of the sequence after 40 iterations?} 9 years ago The puzzle asks us to compute the @italic{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 @racket[1] becomes ``one 1'', written @racket[11]; @racket[11] becomes ``two ones'', or @racket[21], then @racket[1211], @racket[111221], and so on. 9 years ago As in @secref{Day_1}, this puzzle relies on cumulative state, so we'll loop using @iracket[for/fold]. To generate the new string for each pass of the loop, we'll use @iracket[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. 9 years ago 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. @chunk[ (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 5 years ago (append-map (λ (digit-run) 9 years ago (list (string-length digit-run) (substring digit-run 0 1))) digit-runs))))) ] @chunk[ (define (q1 input-key) (string-length (look-and-say 40 input-key)))] @section{After 50 iterations?} We use the same @racket[look-and-say] function, but with an iteration argument of @racket[50] rather than @racket[40]. @chunk[ (define (q2 input-key) (string-length (look-and-say 50 input-key))) ] @section{Testing Day 10} @chunk[ (module+ test (define input-key (file->string "day10-input.txt")) (check-equal? (q1 input-key) 492982) (check-equal? (q2 input-key) 6989950))]