day 17: more cleanup...
This commit is contained in:
parent
a0bf7d0039
commit
e7d9c9d0d3
29
17.rkt
29
17.rkt
|
@ -26,29 +26,21 @@
|
||||||
(define nearby/3d (filter #{zero? (pt-w %)} nearby/4d))
|
(define nearby/3d (filter #{zero? (pt-w %)} nearby/4d))
|
||||||
|
|
||||||
(define (calc-bounds world)
|
(define (calc-bounds world)
|
||||||
(define (helper getter reducer)
|
(define (helper reducer)
|
||||||
(apply reducer (set-map world getter)))
|
(apply pt (map #{apply reducer (set-map world %)} pt-getters)))
|
||||||
(define (helper2 reducer)
|
(values (helper min) (helper max)))
|
||||||
(apply pt (map #{helper % reducer} pt-getters)))
|
|
||||||
(values (helper2 min) (helper2 max)))
|
|
||||||
|
|
||||||
(define (simulate world use-w?)
|
(define (simulate world use-w?)
|
||||||
(define new-world (mutable-set))
|
|
||||||
(define-values [lower upper] (calc-bounds world))
|
(define-values [lower upper] (calc-bounds world))
|
||||||
(define nearby (if use-w? nearby/4d nearby/3d))
|
(define nearby (if use-w? nearby/4d nearby/3d))
|
||||||
|
|
||||||
(for ([pt (in-pts lower upper use-w?)])
|
(for*/set ([pt (in-pts lower upper use-w?)]
|
||||||
(define ct
|
[ct (in-value (stream-count #{set-member? world (pt+ pt %)} (in-list nearby)))]
|
||||||
(for*/sum ([nb (in-list nearby)])
|
#:when (if (set-member? world pt) (<= 2 ct 3) (= 3 ct)))
|
||||||
(if (set-member? world (pt+ pt nb)) 1 0)))
|
pt))
|
||||||
(when (if (set-member? world pt) (<= 2 ct 3) (= 3 ct))
|
|
||||||
(set-add! new-world pt)))
|
|
||||||
|
|
||||||
new-world)
|
|
||||||
|
|
||||||
(define (simulate/times world times use-w?)
|
(define (simulate/times world times use-w?)
|
||||||
(set-count
|
(set-count (for/fold ([world world]) ([_ (in-range times)])
|
||||||
(for/fold ([world world]) ([_ (in-range times)])
|
|
||||||
(simulate world use-w?))))
|
(simulate world use-w?))))
|
||||||
|
|
||||||
(define part1 #{simulate/times % 6 #f})
|
(define part1 #{simulate/times % 6 #f})
|
||||||
|
@ -64,11 +56,6 @@
|
||||||
(set-add! world (pt x y 0 0)))))
|
(set-add! world (pt x y 0 0)))))
|
||||||
world)
|
world)
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
;; tests here
|
|
||||||
(displayln "no tests :("))
|
|
||||||
|
|
||||||
(module+ main
|
(module+ main
|
||||||
(define input (parse "inputs/17"))
|
(define input (parse "inputs/17"))
|
||||||
(answer 17 1 (time (part1 input)))
|
(answer 17 1 (time (part1 input)))
|
||||||
|
|
Loading…
Reference in New Issue