day 15: switch to vector for more good perf
This commit is contained in:
parent
8517fd05cc
commit
27b79d79ad
10
15.rkt
10
15.rkt
|
@ -5,20 +5,20 @@
|
||||||
;; solution for day 15
|
;; solution for day 15
|
||||||
|
|
||||||
(define (naive-solution input limit)
|
(define (naive-solution input limit)
|
||||||
(define seen-numbers (make-hash))
|
(define seen-numbers (make-vector limit #f))
|
||||||
(define prev
|
(define prev
|
||||||
(for/last ([num (in-list input)] [i (in-naturals)])
|
(for/last ([num (in-list input)] [i (in-naturals)])
|
||||||
(begin0 num (hash-set! seen-numbers num i))))
|
(begin0 num (vector-set! seen-numbers num i))))
|
||||||
;; delay "seeing" numbers by one iteration
|
;; delay "seeing" numbers by one iteration
|
||||||
(hash-remove! seen-numbers prev)
|
(vector-set! seen-numbers prev #f)
|
||||||
|
|
||||||
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
||||||
(begin0
|
(begin0
|
||||||
(match (hash-ref seen-numbers prev #f)
|
(match (vector-ref seen-numbers prev)
|
||||||
[#f 0]
|
[#f 0]
|
||||||
[idx (- i 1 idx)])
|
[idx (- i 1 idx)])
|
||||||
;; _now_ we've "seen" it, but it happened on the last turn
|
;; _now_ we've "seen" it, but it happened on the last turn
|
||||||
(hash-set! seen-numbers prev (sub1 i)))))
|
(vector-set! seen-numbers prev (sub1 i)))))
|
||||||
|
|
||||||
(define part1 #{naive-solution % 2020})
|
(define part1 #{naive-solution % 2020})
|
||||||
(define part2 #{naive-solution % 30000000})
|
(define part2 #{naive-solution % 30000000})
|
||||||
|
|
Loading…
Reference in New Issue