diff --git a/aoc-racket.scrbl b/aoc-racket.scrbl index 395b687..8c3f2c4 100644 --- a/aoc-racket.scrbl +++ b/aoc-racket.scrbl @@ -44,4 +44,5 @@ You can install this package (if you haven't already) with @include-section[(submod "day21.rkt" doc)] @include-section[(submod "day22.rkt" doc)] @include-section[(submod "day23.rkt" doc)] -@include-section[(submod "day24.rkt" doc)] \ No newline at end of file +@include-section[(submod "day24.rkt" doc)] +@include-section[(submod "day25.rkt" doc)] \ No newline at end of file diff --git a/day25-input.txt b/day25-input.txt new file mode 100644 index 0000000..c697b57 --- /dev/null +++ b/day25-input.txt @@ -0,0 +1 @@ +To continue, please consult the code grid in the manual. Enter the code at row 2947, column 3029. \ No newline at end of file diff --git a/day25.rkt b/day25.rkt new file mode 100644 index 0000000..608a99f --- /dev/null +++ b/day25.rkt @@ -0,0 +1,80 @@ +#lang scribble/lp2 +@(require scribble/manual aoc-racket/helper) + +@aoc-title[25] + +@defmodule[aoc-racket/day25] + +@link["http://adventofcode.com/day/25"]{The puzzle}. Our @link-rp["day25-input.txt"]{input} is a row and column number for a grid. + + +@chunk[ + + + ] + +@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[ + (require racket rackunit) + (provide (all-defined-out)) + ] + + +@chunk[ + + (define first-code 20151125) + + (define (next-code code) + (modulo (* code 252533) 33554393)) + + (define (nth-code n) + (for/fold ([code-so-far first-code]) + ([i (in-range (sub1 n))]) + (next-code code-so-far))) + + (define (rc->n row col) + (define first-col-val (add1 (apply + (range row)))) + (define col-offset-val (apply + (range (add1 row) (+ row col)))) + (+ first-col-val col-offset-val)) + + (define (q1 input-str) + (match-define (list _ row col) + (map string->number + (regexp-match #px"row (\\d+), column (\\d+)" input-str))) + (nth-code (rc->n row col))) + + ] + + + +@section{Testing Day 25} + +@chunk[ + (module+ test + (define input-str (file->string "day25-input.txt")) + (check-equal? (q1 input-str) 19980801))] + + diff --git a/info.rkt b/info.rkt index 94c3cf9..04fdb3e 100644 --- a/info.rkt +++ b/info.rkt @@ -2,4 +2,5 @@ (define collection "aoc-racket") (define scribblings '(("aoc-racket.scrbl" (multi-page)))) (define deps '("base" "scribble-lib" "sugar" "rackunit-lib")) +(define test-omit-paths (list #rx"rkt$")) (define build-deps '("rackunit-lib" "racket-doc" "scribble-doc" "rackunit-doc" "at-exp-lib")) \ No newline at end of file