Add Advent 2021 - day 11.
This commit is contained in:
parent
a28a3d1c6e
commit
31e1fa2682
|
@ -0,0 +1,53 @@
|
|||
#lang racket/base
|
||||
(require racket/match)
|
||||
|
||||
(define (char->number c)
|
||||
(string->number (string c)))
|
||||
|
||||
(define (make-line t)
|
||||
(list->vector (map char->number (string->list t))))
|
||||
|
||||
(define sv (for/vector ([t (in-lines)]) (make-line t)))
|
||||
|
||||
(define v (vector-length sv))
|
||||
(define h (vector-length (vector-ref sv 0)))
|
||||
|
||||
(define (access pv ph)
|
||||
(vector-ref (vector-ref sv pv) ph))
|
||||
(define (change pv ph v)
|
||||
(vector-set! (vector-ref sv pv) ph v))
|
||||
|
||||
(define (apply-step i)
|
||||
; keep track of octopuses that have flashed
|
||||
(define index 0)
|
||||
(define nines (make-vector 300 null)) ; arbitrary length
|
||||
(define (try-add-nine pv ph)
|
||||
(when (= (access pv ph) 9)
|
||||
(vector-set! nines index (cons pv ph))
|
||||
(set! index (add1 index)))
|
||||
(change pv ph (add1 (access pv ph))))
|
||||
|
||||
(for* ([vi (in-range v)] [hi (in-range h)])
|
||||
(when (> (access vi hi) 9) (change vi hi 0)) ; optimization to make reset simpler
|
||||
(try-add-nine vi hi))
|
||||
|
||||
; loop through nines but being able to add more flashes
|
||||
(for ([j (in-naturals)] #:break (= j index))
|
||||
(match-define (cons pv ph) (vector-ref nines j))
|
||||
|
||||
(define (touch-point pv ph)
|
||||
(when (and (and (>= pv 0) (< pv v)) (and (>= ph 0) (< ph h)))
|
||||
(try-add-nine pv ph)))
|
||||
|
||||
(for* ([dv (in-range -1 2)] [dh (in-range -1 2)] #:unless (= 0 dv dh))
|
||||
(touch-point (+ pv dv) (+ ph dh))))
|
||||
|
||||
index)
|
||||
|
||||
; part 1
|
||||
(for/sum ([i (in-range 100)])
|
||||
(apply-step i))
|
||||
; part 2
|
||||
(for/last ([i (in-naturals)])
|
||||
#:break (= (apply-step i) (* v h))
|
||||
(+ i 100 2))
|
|
@ -0,0 +1,10 @@
|
|||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
|
@ -0,0 +1,10 @@
|
|||
4721224663
|
||||
6875415276
|
||||
2742448428
|
||||
4878231556
|
||||
5684643743
|
||||
3553681866
|
||||
4788183625
|
||||
4255856532
|
||||
1415818775
|
||||
2326886125
|
Loading…
Reference in New Issue