day 15: somehow majorly improve speed with begin0

This commit is contained in:
xenia 2020-12-15 00:37:12 -05:00
parent 8b17fe1d26
commit 8517fd05cc
1 changed files with 5 additions and 7 deletions

12
15.rkt
View File

@ -8,19 +8,17 @@
(define seen-numbers (make-hash)) (define seen-numbers (make-hash))
(define prev (define prev
(for/last ([num (in-list input)] [i (in-naturals)]) (for/last ([num (in-list input)] [i (in-naturals)])
(hash-set! seen-numbers num i) (begin0 num (hash-set! seen-numbers num i))))
num))
;; delay "seeing" numbers by one iteration ;; delay "seeing" numbers by one iteration
(hash-remove! seen-numbers prev) (hash-remove! seen-numbers prev)
(for/fold ([prev prev]) ([i (in-range (length input) limit)]) (for/fold ([prev prev]) ([i (in-range (length input) limit)])
(define new-val (begin0
(match (hash-ref seen-numbers prev #f) (match (hash-ref seen-numbers prev #f)
[#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)) (hash-set! seen-numbers prev (sub1 i)))))
new-val))
(define part1 #{naive-solution % 2020}) (define part1 #{naive-solution % 2020})
(define part2 #{naive-solution % 30000000}) (define part2 #{naive-solution % 30000000})