From 8517fd05ccbf42b33817a4cc63fd47e333a11494 Mon Sep 17 00:00:00 2001 From: haskal Date: Tue, 15 Dec 2020 00:37:12 -0500 Subject: [PATCH] day 15: somehow majorly improve speed with begin0 --- 15.rkt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/15.rkt b/15.rkt index 6705ebb..34e602e 100644 --- a/15.rkt +++ b/15.rkt @@ -8,19 +8,17 @@ (define seen-numbers (make-hash)) (define prev (for/last ([num (in-list input)] [i (in-naturals)]) - (hash-set! seen-numbers num i) - num)) + (begin0 num (hash-set! seen-numbers num i)))) ;; delay "seeing" numbers by one iteration (hash-remove! seen-numbers prev) (for/fold ([prev prev]) ([i (in-range (length input) limit)]) - (define new-val + (begin0 (match (hash-ref seen-numbers prev #f) [#f 0] - [idx (- i 1 idx)])) - ;; _now_ we've "seen" it, but it happened on the last turn - (hash-set! seen-numbers prev (sub1 i)) - new-val)) + [idx (- i 1 idx)]) + ;; _now_ we've "seen" it, but it happened on the last turn + (hash-set! seen-numbers prev (sub1 i))))) (define part1 #{naive-solution % 2020}) (define part2 #{naive-solution % 30000000})