day 15: use unsafe for more fast
This commit is contained in:
parent
27b79d79ad
commit
ef930612d7
16
15.rkt
16
15.rkt
|
@ -4,21 +4,23 @@
|
||||||
|
|
||||||
;; solution for day 15
|
;; solution for day 15
|
||||||
|
|
||||||
|
(require racket/fixnum racket/unsafe/ops)
|
||||||
|
|
||||||
(define (naive-solution input limit)
|
(define (naive-solution input limit)
|
||||||
(define seen-numbers (make-vector limit #f))
|
(define seen-numbers (make-fxvector limit -1))
|
||||||
(define prev
|
(define prev
|
||||||
(for/last ([num (in-list input)] [i (in-naturals)])
|
(for/last ([num (in-list input)] [i (in-naturals)])
|
||||||
(begin0 num (vector-set! seen-numbers num i))))
|
(begin0 num (unsafe-fxvector-set! seen-numbers num i))))
|
||||||
;; delay "seeing" numbers by one iteration
|
;; delay "seeing" numbers by one iteration
|
||||||
(vector-set! seen-numbers prev #f)
|
(unsafe-fxvector-set! seen-numbers prev -1)
|
||||||
|
|
||||||
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
||||||
(begin0
|
(begin0
|
||||||
(match (vector-ref seen-numbers prev)
|
(match (unsafe-fxvector-ref seen-numbers prev)
|
||||||
[#f 0]
|
[-1 0]
|
||||||
[idx (- i 1 idx)])
|
[idx (unsafe-fx- 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
|
||||||
(vector-set! seen-numbers prev (sub1 i)))))
|
(unsafe-fxvector-set! seen-numbers prev (unsafe-fx- i 1)))))
|
||||||
|
|
||||||
(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