1
0
Fork 0

Day 11: Use sequences instead of lists. Slight speedup, I guess.

This commit is contained in:
Jonathan Chan 2020-12-10 23:22:08 -08:00 committed by Jonathan Chan
parent 488e3dc517
commit 1e2c6a0b8b
1 changed files with 10 additions and 10 deletions

View File

@ -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))])