diff options
author | HJ <thechairman@thechairman.info> | 2021-12-11 23:24:13 -0500 |
---|---|---|
committer | HJ <thechairman@thechairman.info> | 2021-12-11 23:24:13 -0500 |
commit | 14c955cb4c0e581455aa6bd9d882b94f8b35487d (patch) | |
tree | b6a4847a954559766b4d1dc111049e128f03acf1 /2021 | |
parent | 1855292990b1d423aae36bcf501bc20ddcf6ae82 (diff) | |
download | gleam_aoc-14c955cb4c0e581455aa6bd9d882b94f8b35487d.tar.gz gleam_aoc-14c955cb4c0e581455aa6bd9d882b94f8b35487d.zip |
day 11
Diffstat (limited to '2021')
-rw-r--r-- | 2021/day-11/day-11.rkt | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/2021/day-11/day-11.rkt b/2021/day-11/day-11.rkt new file mode 100644 index 0000000..0a3d485 --- /dev/null +++ b/2021/day-11/day-11.rkt @@ -0,0 +1,79 @@ +#lang racket +(require "../../jj-aoc.rkt" + threading) + +(define coords + (~>> (for/list ([r (in-range 10)]) + (for/list ([c (in-range 10)]) + (cons r c))) + (apply append))) + +(define octopus-data + (~>> (for/list ([l (in-lines (open-day 11 2021))] + #:unless (equal? l "")) + (~>> l + string->list + (map (λ~>> ~a string->number)))) + (apply append) + (map cons coords) + make-hash)) + +(define total-length (hash-count octopus-data)) +(define row-length (sqrt total-length)) + +(define (adjacent-to coord) + (match-define (cons r c) coord) + (for*/list ([row (in-list '(-1 0 1))] + [col (in-list '(-1 0 1))] + #:unless (= 0 row col)) + (cons (+ r row) (+ c col)))) + +(define (simulate-octopi-step data) + (define flashed-this-step (mutable-set)) + + (let look-for-more-flashes + ([octopi (for/hash ([(k v) data]) + (values k (add1 v)))] + [flashes-so-far 0]) + (define-values (next-octopus-update + flashes-this-update) + (for*/fold ([octopi octopi] [flashes 0]) + ([(p x) (in-hash octopi)] + #:when (> x 9) + #:unless (set-member? flashed-this-step p)) + (set-add! flashed-this-step p) + (define flashed-octopi + (for*/fold ([o (hash-set octopi p 0)]) + ([adj (in-list (adjacent-to p))] + #:when (hash-has-key? o adj) + #:unless (set-member? flashed-this-step adj)) + (hash-update o adj add1))) + (values flashed-octopi + (add1 flashes)))) + (if (> flashes-this-update 0) + (look-for-more-flashes next-octopus-update + (+ flashes-so-far + flashes-this-update)) + (values next-octopus-update + flashes-so-far)))) + +;; part 1 +(for/fold ([octopi octopus-data] + [total-flashes 0] #:result total-flashes) + ([step (in-range 100)]) + (define-values [next-state + flashes-from-this-state] + (simulate-octopi-step octopi)) + (values next-state + (+ total-flashes flashes-from-this-state))) + +;; part 2 +(for/fold ([octopi octopus-data] + [synchro-step #f] #:result synchro-step) + ([step (in-naturals 1)]) + (define-values [next-state + flashes-from-this-state] + (simulate-octopi-step octopi)) + #:final (= flashes-from-this-state 100) + (values next-state + step)) |