From adf8ba46006a4cdcea45ec8baa1b62c39883c4a9 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Fri, 10 Dec 2021 22:21:12 -0800 Subject: [PATCH] Day 11 (with mutation...). --- input/11.txt | 10 ++++++++ lib.rkt | 11 ++++++++- src/11.rkt | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 input/11.txt create mode 100644 src/11.rkt diff --git a/input/11.txt b/input/11.txt new file mode 100644 index 0000000..cfbdc33 --- /dev/null +++ b/input/11.txt @@ -0,0 +1,10 @@ +5421451741 +3877321568 +7583273864 +3451717778 +2651615156 +6377167526 +5182852831 +4766856676 +3437187583 +3633371586 diff --git a/lib.rkt b/lib.rkt index ae39990..8280a40 100644 --- a/lib.rkt +++ b/lib.rkt @@ -190,10 +190,19 @@ [else #f])) ;; snoc : (listof any) -> any -> (listof any) -;; Append element to the back of the list. +;; Append element to the back of the list (define (snoc lst v) (append lst (list v))) +;; range* : number? -> (listof number?) +;; range* : number? -> number? -> [number?] -> (listof number?) +;; Like range, but with an inclusive right end +(define-syntax range* + (syntax-rules () + [(_ n) (range (add1 n))] + [(_ from to) (range from (add1 to))] + [(_ from to step) (range from (add1 to) step)])) + ;; scanl : (a -> a -> a) -> (listof a) -> (listof a) ;; foldl that accumulates partial results in a list (define (scanl f init lst) diff --git a/src/11.rkt b/src/11.rkt new file mode 100644 index 0000000..865f614 --- /dev/null +++ b/src/11.rkt @@ -0,0 +1,68 @@ +#lang curly-fn racket + +(require "../lib.rkt") + +(define input + (for/vector ([row (problem-input 11)]) + (for/vector ([col (string->list row)]) + (char->number col)))) + +(define rows (vector-length input)) +(define cols (vector-length (vector-ref input 0))) +(define total (* rows cols)) + +(define (grid-ref grid row col) + (vector-ref (vector-ref grid row) col)) + +(define (grid-update! grid row col f) + (vector-set! (vector-ref grid row) col (f (grid-ref grid row col)))) + +(define (adjs grid row col) + (for*/list ([r (range* (max 0 (sub1 row)) (min (sub1 rows) (add1 row)))] + [c (range* (max 0 (sub1 col)) (min (sub1 cols) (add1 col)))]) + (cons r c))) + +(define (incr! octopodes) + (for ([row (range rows)]) + (for ([col (range cols)]) + (grid-update! octopodes row col add1)))) + +(define (flash! flashed octopodes) + (for*/fold ([newly-flashed '()]) + ([row (range rows)] + [col (range cols)]) + (if (and (not (member (cons row col) flashed)) + (> (grid-ref octopodes row col) 9) + (for ([adj (adjs octopodes row col)]) + (grid-update! octopodes (car adj) (cdr adj) add1))) + (cons (cons row col) newly-flashed) + newly-flashed))) + +(define (decharge! octopodes) + (for ([row (range rows)]) + (for ([col (range cols)]) + (when (> (grid-ref octopodes row col) 9) + (grid-update! octopodes row col (const 0)))))) + +(define (step! octopodes) + (incr! octopodes) + (define flashes + (let loop ([flashed '()]) + (let ([newly-flashed (flash! flashed octopodes)]) + (if (empty? newly-flashed) + (length flashed) + (loop (append newly-flashed flashed)))))) + (decharge! octopodes) + flashes) + +(define part1 + (for/sum ([_ (range 100)]) + (step! input))) + +(define part2 + (let loop ([steps 101]) + (if (= (step! input) total) + steps + (loop (add1 steps))))) + +(show-solution part1 part2) \ No newline at end of file