Minor refactoring for Day 17.
This commit is contained in:
parent
7f1fa39d7c
commit
24630da977
23
src/17.rkt
23
src/17.rkt
|
@ -7,29 +7,32 @@
|
||||||
|
|
||||||
(define grid
|
(define grid
|
||||||
(for*/fold ([grid (hash)])
|
(for*/fold ([grid (hash)])
|
||||||
([x 8]
|
([x (length (first input))]
|
||||||
[y 8])
|
[y (length input)])
|
||||||
(hash-set grid (list x y 0 0) (list-ref (list-ref input y) x))))
|
(hash-set grid (list x y 0 0) (list-ref (list-ref input y) x))))
|
||||||
|
|
||||||
(define (actives chars)
|
(define (actives chars)
|
||||||
(count #{char=? % #\#} chars))
|
(count #{char=? % #\#} chars))
|
||||||
|
|
||||||
(define (cycle part1? grid)
|
(define (cycle part1? grid)
|
||||||
|
(define (in-range+ from to)
|
||||||
|
(in-range (- from 1) (+ to 2)))
|
||||||
(let* ([coords (hash-keys grid)]
|
(let* ([coords (hash-keys grid)]
|
||||||
[xs (map first coords)] [min-x (apply min xs)] [max-x (apply max xs)]
|
[xs (map first coords)] [min-x (apply min xs)] [max-x (apply max xs)]
|
||||||
[ys (map second coords)] [min-y (apply min ys)] [max-y (apply max ys)]
|
[ys (map second coords)] [min-y (apply min ys)] [max-y (apply max ys)]
|
||||||
[zs (map third coords)] [min-z (apply min zs)] [max-z (apply max zs)]
|
[zs (map third coords)] [min-z (apply min zs)] [max-z (apply max zs)]
|
||||||
[ws (map fourth coords)] [min-w (apply min ws)] [max-w (apply max ws)])
|
[ws (map fourth coords)] [min-w (apply min ws)] [max-w (apply max ws)])
|
||||||
(for*/fold ([grid* grid])
|
(for*/fold ([grid* grid])
|
||||||
([x (in-range (- min-x 1) (+ max-x 2))]
|
([x (in-range+ min-x max-x)]
|
||||||
[y (in-range (- min-y 1) (+ max-y 2))]
|
[y (in-range+ min-y max-y)]
|
||||||
[z (in-range (- min-z 1) (+ max-z 2))]
|
[z (in-range+ min-z max-z)]
|
||||||
[w (if part1? '(0) (in-range (- min-w 1) (+ max-w 2)))])
|
[w (if part1? '(0) (in-range+ min-w max-w))])
|
||||||
(let* ([cube (hash-ref grid (list x y z w) #\.)]
|
(let* ([cube (hash-ref grid (list x y z w) #\.)]
|
||||||
[neighbours (for*/list ([x* (in-range (- x 1) (+ x 2))]
|
[neighbours
|
||||||
[y* (in-range (- y 1) (+ y 2))]
|
(for*/list ([x* (in-range+ x x)]
|
||||||
[z* (in-range (- z 1) (+ z 2))]
|
[y* (in-range+ y y)]
|
||||||
[w* (if part1? '(0) (in-range (- w 1) (+ w 2)))]
|
[z* (in-range+ z z)]
|
||||||
|
[w* (if part1? '(0) (in-range+ w w))]
|
||||||
#:unless (and (= x x*) (= y y*) (= z z*) (= w w*)))
|
#:unless (and (= x x*) (= y y*) (= z z*) (= w w*)))
|
||||||
(hash-ref grid (list x* y* z* w*) #\.))]
|
(hash-ref grid (list x* y* z* w*) #\.))]
|
||||||
[active-neighbours (actives neighbours)])
|
[active-neighbours (actives neighbours)])
|
||||||
|
|
Loading…
Reference in New Issue