From ef930612d755da8341789b1b5ce8154c03ee8f51 Mon Sep 17 00:00:00 2001 From: haskal Date: Tue, 15 Dec 2020 02:03:03 -0500 Subject: [PATCH] day 15: use unsafe for more fast --- 15.rkt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/15.rkt b/15.rkt index ab7bc24..1fff38d 100644 --- a/15.rkt +++ b/15.rkt @@ -4,21 +4,23 @@ ;; solution for day 15 +(require racket/fixnum racket/unsafe/ops) + (define (naive-solution input limit) - (define seen-numbers (make-vector limit #f)) + (define seen-numbers (make-fxvector limit -1)) (define prev (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 - (vector-set! seen-numbers prev #f) + (unsafe-fxvector-set! seen-numbers prev -1) (for/fold ([prev prev]) ([i (in-range (length input) limit)]) (begin0 - (match (vector-ref seen-numbers prev) - [#f 0] - [idx (- i 1 idx)]) + (match (unsafe-fxvector-ref seen-numbers prev) + [-1 0] + [idx (unsafe-fx- i 1 idx)]) ;; _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 part2 #{naive-solution % 30000000})