You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('') and can be up to 35 characters long.
66 lines
2.5 KiB
66 lines
2.5 KiB
#lang scribble/lp2


@(require scribble/manual aocracket/helper)




@aoctitle[10]




@defmodule[aocracket/day10]




@link["http://adventofcode.com/day/10"]{The puzzle}. Our @linkrp["day10input.txt"]{input} is a short numeric key.




@chunk[<day10>


<day10setup>


<day10q1>


<day10q2>


<day10test>]




@isection{What's the length of the sequence after 40 iterations?}




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.




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[regexpmatch*] 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.




@chunk[<day10setup>


(require racket rackunit)


(provide (alldefinedout))




(define (lookandsay iterations inputkey)


(for/fold ([start inputkey])


([i (inrange iterations)])


(define digitruns (regexpmatch* #px"(\\d)\\1*" start))


(stringappend*


(map ~a


(appendmap (λ (digitrun)


(list (stringlength digitrun)


(substring digitrun 0 1)))


digitruns)))))




]




@chunk[<day10q1>




(define (q1 inputkey)


(stringlength (lookandsay 40 inputkey)))]








@section{After 50 iterations?}




We use the same @racket[lookandsay] function, but with an iteration argument of @racket[50] rather than @racket[40].




@chunk[<day10q2>




(define (q2 inputkey)


(stringlength (lookandsay 50 inputkey))) ]






@section{Testing Day 10}




@chunk[<day10test>


(module+ test


(define inputkey (file>string "day10input.txt"))


(checkequal? (q1 inputkey) 492982)


(checkequal? (q2 inputkey) 6989950))]






