Refactored Day 15: Using vector instead of hash.
This commit is contained in:
parent
4a858eca81
commit
9c9511a05b
16
src/15.rkt
16
src/15.rkt
|
@ -5,17 +5,19 @@
|
||||||
(define input '(5 1 9 18 13 8 0))
|
(define input '(5 1 9 18 13 8 0))
|
||||||
|
|
||||||
(define (play end)
|
(define (play end)
|
||||||
(define turns
|
(define turns (make-vector end #f))
|
||||||
(make-hash (map cons input (range 1 (add1 (length input))))))
|
(for-each #{vector-set! turns %1 (add1 %2)}
|
||||||
(let loop ([turn (length input)] [curr (last input)])
|
input (range (length input)))
|
||||||
|
(let loop ([turn (length input)]
|
||||||
|
[curr (last input)])
|
||||||
(cond
|
(cond
|
||||||
[(>= turn end) curr]
|
[(>= turn end) curr]
|
||||||
[(hash-has-key? turns curr)
|
[(vector-ref turns curr)
|
||||||
(let ([next (- turn (hash-ref turns curr))])
|
(let ([next (- turn (vector-ref turns curr))])
|
||||||
(hash-set! turns curr turn)
|
(vector-set! turns curr turn)
|
||||||
(loop (add1 turn) next))]
|
(loop (add1 turn) next))]
|
||||||
[else
|
[else
|
||||||
(hash-set! turns curr turn)
|
(vector-set! turns curr turn)
|
||||||
(loop (add1 turn) 0)])))
|
(loop (add1 turn) 0)])))
|
||||||
|
|
||||||
(show-solution (play 2020) (play 30000000))
|
(show-solution (play 2020) (play 30000000))
|
||||||
|
|
Loading…
Reference in New Issue