diff --git a/src/15.rkt b/src/15.rkt index d283602..7204a81 100644 --- a/src/15.rkt +++ b/src/15.rkt @@ -5,17 +5,19 @@ (define input '(5 1 9 18 13 8 0)) (define (play end) - (define turns - (make-hash (map cons input (range 1 (add1 (length input)))))) - (let loop ([turn (length input)] [curr (last input)]) + (define turns (make-vector end #f)) + (for-each #{vector-set! turns %1 (add1 %2)} + input (range (length input))) + (let loop ([turn (length input)] + [curr (last input)]) (cond [(>= turn end) curr] - [(hash-has-key? turns curr) - (let ([next (- turn (hash-ref turns curr))]) - (hash-set! turns curr turn) + [(vector-ref turns curr) + (let ([next (- turn (vector-ref turns curr))]) + (vector-set! turns curr turn) (loop (add1 turn) next))] [else - (hash-set! turns curr turn) + (vector-set! turns curr turn) (loop (add1 turn) 0)]))) (show-solution (play 2020) (play 30000000))