

#lang scribble/lp2



@(require scribble/manual aocracket/helper)






@aoctitle[25]






@defmodule[aocracket/day25]






@link["http://adventofcode.com/day/25"]{The puzzle}. Our @linkrp["day25input.txt"]{input} is a row and column number for a grid.









@chunk[<day25>



<day25setup>



<day25q1>



<day25test>]






@section{What code do you give the machine?}






The puzzle involves a series of codes in a grid. The first code is @racket[20151125] and subsequent codes are computed by multiplying by @racket[252533] and calculating the remainder after dividing by @racket[33554393]. Then the codes are put into a grid in diagonal fashion, in the order shown:






@tt{



 1 2 3 4 5 6



@(linebreak)



++++++++++++++++++++++ @(linebreak)






1  1 3 6 10 15 21 @(linebreak)






2  2 5 9 14 20 @(linebreak)






3  4 8 13 19 @(linebreak)






4  7 12 18 @(linebreak)






5  11 17 @(linebreak)






6  16}






The grid is described as infinite. But this doesn't play a role in the puzzle, since our job is to find the number at the finite coordinate given in our input. So we need a function for finding the @italic{n}th code in the series, and a function for finding which @italic{n} resides at a given grid location. Short & sweet.









@chunk[<day25setup>



(require racket rackunit)



(provide (alldefinedout))



]









@chunk[<day25q1>






(define firstcode 20151125)






(define (nextcode code)



(modulo (* code 252533) 33554393))






(define (nthcode n)



(for/fold ([codesofar firstcode])



([i (inrange (sub1 n))])



(nextcode codesofar)))






(define (rc>n row col)



(define firstcolval (add1 (apply + (range row))))



(define coloffsetval (apply + (range (add1 row) (+ row col))))



(+ firstcolval coloffsetval))






(define (q1 inputstr)



(matchdefine (list _ row col)



(map string>number



(regexpmatch #px"row (\\d+), column (\\d+)" inputstr)))



(nthcode (rc>n row col)))






]












@section{Testing Day 25}






@chunk[<day25test>



(module+ test



(define inputstr (file>string "day25input.txt"))



(checkequal? (q1 inputstr) 19980801))]






