From 7c6f382dc1f9f50b8b45adf19b871ca0b38cd342 Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Sat, 11 Dec 2021 19:10:06 -0500 Subject: [PATCH] day11 --- day11.rkt | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ day11.test.txt | 10 ++++++ day11.txt | 10 ++++++ 3 files changed, 104 insertions(+) create mode 100644 day11.rkt create mode 100644 day11.test.txt create mode 100644 day11.txt diff --git a/day11.rkt b/day11.rkt new file mode 100644 index 0000000..0e9e695 --- /dev/null +++ b/day11.rkt @@ -0,0 +1,84 @@ +#lang racket + +(define SIZE 10); + +(define (map-map f lst) ; map . map + (map (λ (x) (map (λ (y) (f y)) x)) lst)) + +(define (char->num chr) ; i hate this + (second (assoc chr (list '(#\0 0) '(#\1 1) '(#\2 2) '(#\3 3) '(#\4 4) '(#\5 5) '(#\6 6) '(#\7 7) '(#\8 8) '(#\9 9))))) + +(define (subtract l1 l2) ; retun list of items that are only in l1 + (filter (λ (x) (not (member x l2))) l1)) + +(define grid ; file -> flat vector of the grid digits + (list->vector + (flatten + (map-map char->num + (map string->list + (file->lines "day11.txt")))))) + +(define (display-grid grid) + (for-each (λ (i) + (printf "~s " (vector-ref grid i)) + (if (= 0 (modulo (add1 i) SIZE)) (printf "~n") (printf ""))) + (range 0 (* SIZE SIZE)))) + +(define (neighbors idx) ; find all the neighboring indicies, filtering out out-of-bounds + (filter (λ (idx) (and (>= idx 0) (< idx (* SIZE SIZE)))) ; remove ones off the top or bottom of board + (filter identity ; remove #f + (flatten (list (if (= (modulo idx SIZE) 0) #f ; if it's on the left edge, #f + (list (- idx (add1 SIZE)) (+ idx (sub1 SIZE)) (sub1 idx))) + (if (= (modulo (add1 idx) SIZE) 0) #f ; if it's on the right edge, #f + (list (- idx (sub1 SIZE)) (+ idx (add1 SIZE)) (add1 idx))) + (list (- idx SIZE) (+ idx SIZE))))))) + +(define (flash grid tens seent flash-count) ; recursively flash tens + (for-each (λ (i) (hash-set! seent i #t)) tens) ; mark these tens as having been seen + (let* ([ns (flatten (map neighbors tens))] ; find all the neighbors of the tens + [ns-filtered (filter (λ (i) (not (hash-has-key? seent i))) ns)] ; filter out the ones we've flashed + [new-flash-count (+ flash-count (length tens))]) + (for-each (λ (i) (vector-set! grid i (add1 (vector-ref grid i)))) ns-filtered) ; add 1 to all the neighbors + (let* ([new-tens (remove-duplicates (filter (λ (x) (<= 10 (vector-ref grid x))) ns-filtered))]) + (if (< 0 (length new-tens)) + (flash grid new-tens seent new-flash-count) ; if so, flash em + (list new-flash-count (vector-map! (λ (x) (if (>= x 10) 0 x)) grid)))))) ; otherwise set our 10s to 0 and return the vector + +(define (vector-find vect proc) ; returns list of indicies where val is found in vect + (foldr (λ (i acc) (if (proc (vector-ref vect i)) (cons i acc) acc)) '() (range 0 (* SIZE SIZE)))) + +(define (step grid) ; grid is actually '(count grid).... + (let* ([g (second grid)] + [flash-count (first grid)] + [incd (vector-map add1 g)] + [seent (make-hash)] + [tens (vector-find incd (λ (n) (>= n 10)))] + [res (flash incd tens seent flash-count)]) + res)) + +(define part1 + (let loop ([n 100] [g grid] [flash-count 0]) + (if (= n 0) + (list g flash-count) + (let* ([res (step (list flash-count g))] + [new-count (first res)] + [new-grid (second res)]) + (loop (sub1 n) new-grid new-count))))) + +part1 +(display-grid (first part1)) + +(define (all-flashing? grid) + (= 100 + (vector-length (vector-filter (λ (x) (= x 0)) grid)))) + +(define part2 + (+ 1 (let loop ([n 0] [g grid] [flash-count 0]) + (let* ([res (step (list flash-count g))] + [new-count (first res)] + [new-grid (second res)]) + (if (all-flashing? new-grid) + n + (loop (add1 n) new-grid new-count)))))) + +part2 \ No newline at end of file diff --git a/day11.test.txt b/day11.test.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/day11.test.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/day11.txt b/day11.txt new file mode 100644 index 0000000..a769718 --- /dev/null +++ b/day11.txt @@ -0,0 +1,10 @@ +4438624262 +6263251864 +2618812434 +2134264565 +1815131247 +2612457325 +8585767584 +7217134556 +2825456563 +8248473584