Day 11: Use sequences instead of lists. Slight speedup, I guess.
This commit is contained in:
parent
ad1cbc0e9a
commit
9ea0885bbe
20
src/11.rkt
20
src/11.rkt
|
@ -9,22 +9,22 @@
|
||||||
(define (neighbours seats r c)
|
(define (neighbours seats r c)
|
||||||
(count
|
(count
|
||||||
#{char=? % #\#}
|
#{char=? % #\#}
|
||||||
(for*/list ([r* (range (max 0 (sub1 r)) (min (+ r 2) length))]
|
(for*/list ([r* (in-range (max 0 (sub1 r)) (min (+ r 2) length))]
|
||||||
[c* (range (max 0 (sub1 c)) (min (+ c 2) width))]
|
[c* (in-range (max 0 (sub1 c)) (min (+ c 2) width))]
|
||||||
#:when (not (and (= r r*) (= c c*))))
|
#:when (not (and (= r r*) (= c c*))))
|
||||||
(vector-ref (vector-ref seats r*) c*))))
|
(vector-ref (vector-ref seats r*) c*))))
|
||||||
|
|
||||||
(define (visible-in seats r c dr dc)
|
(define (visible-in seats r c dr dc)
|
||||||
(define rs
|
(define rs
|
||||||
(cond
|
(cond
|
||||||
[(positive? dr) (range r length dr)]
|
[(positive? dr) (in-range r length dr)]
|
||||||
[(negative? dr) (range r -1 dr)]
|
[(negative? dr) (in-range r -1 dr)]
|
||||||
[else (build-list length (const r))]))
|
[else (in-cycle `(,r))]))
|
||||||
(define cs
|
(define cs
|
||||||
(cond
|
(cond
|
||||||
[(positive? dc) (range c width dc)]
|
[(positive? dc) (in-range c width dc)]
|
||||||
[(negative? dc) (range c -1 dc)]
|
[(negative? dc) (in-range c -1 dc)]
|
||||||
[else (build-list width (const c))]))
|
[else (in-cycle `(,c))]))
|
||||||
(for/or ([r* rs]
|
(for/or ([r* rs]
|
||||||
[c* cs]
|
[c* cs]
|
||||||
#:when (not (and (= r r*) (= c c*))))
|
#:when (not (and (= r r*) (= c c*))))
|
||||||
|
@ -33,8 +33,8 @@
|
||||||
(char=? seat #\#)))
|
(char=? seat #\#)))
|
||||||
|
|
||||||
(define (visible seats r c)
|
(define (visible seats r c)
|
||||||
(count identity (map (match-lambda [`(,dr ,dc) (visible-in seats r c dr dc)])
|
(count (match-lambda [`(,dr ,dc) (visible-in seats r c dr dc)])
|
||||||
'((-1 -1) (-1 0) (-1 1) (0 -1) (0 1) (1 -1) (1 0) (1 1)))))
|
'((-1 -1) (-1 0) (-1 1) (0 -1) (0 1) (1 -1) (1 0) (1 1))))
|
||||||
|
|
||||||
(define (step-seats counter die seats)
|
(define (step-seats counter die seats)
|
||||||
(let ([new-seats (vector-map vector-copy (vector-copy seats))])
|
(let ([new-seats (vector-map vector-copy (vector-copy seats))])
|
||||||
|
|
Loading…
Reference in New Issue