▼ Advent of Code:   solutions & explanations
 1 Day 1 2 Day 2 3 Day 3 4 Day 4 5 Day 5 6 Day 6 7 Day 7 8 Day 8 9 Day 9 10 Day 10 11 Day 11 12 Day 12 13 Day 13 14 Day 14 15 Day 15 16 Day 16 17 Day 17 18 Day 18 19 Day 19 20 Day 20 21 Day 21 22 Day 22 23 Day 23 24 Day 24 25 Day 25 Index
 25.1 What code do you give the machine? 25.2 Testing Day 25
7.4

### 25Day 25

 (require aoc-racket/day25) package: aoc-racket

The puzzle. Our input is a row and column number for a grid.



#### 25.1What code do you give the machine?

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

|  1  2  3  4  5  6
++++++++++++++++++++++
1 |  1  3  6 10 15 21
2 |  2  5  9 14 20
3 |  4  8 13 19
4 |  7 12 18
5 | 11 17
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 nth code in the series, and a function for finding which n resides at a given grid location. Short & sweet.

 (require racket rackunit) (provide (all-defined-out))

 (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)))

#### 25.2Testing Day 25

 (module+ test (define input-str (file->string "day25-input.txt")) (check-equal? (q1 input-str) 19980801))