From 3c27a50bb5aca4c018c0320952c5442f64103af5 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Tue, 15 Dec 2020 17:53:32 -0800 Subject: [PATCH] Day 15, but in Typed Racket. --- src/15-typed.rkt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/15-typed.rkt diff --git a/src/15-typed.rkt b/src/15-typed.rkt new file mode 100644 index 0000000..f5d3dbe --- /dev/null +++ b/src/15-typed.rkt @@ -0,0 +1,26 @@ +#lang typed/racket + +(require/typed "../lib.rkt" + [show-solution (-> Any Any Void)]) + +(: input (Listof Natural)) +(define input '(5 1 9 18 13 8 0)) + +(: play (-> Exact-Positive-Integer Integer)) +(define (play end) + (let ([turns : (Vectorof Natural) (make-vector end 0)]) + (for-each (λ ([i : Natural] [j : Natural]) (vector-set! turns i (add1 j))) + input (range (length input))) + (let loop ([turn : Natural (length input)] + [curr : Integer (last input)]) + (cond + [(>= turn end) curr] + [(positive? (vector-ref turns curr)) + (let ([next : Integer (- turn (vector-ref turns curr))]) + (vector-set! turns curr turn) + (loop (add1 turn) next))] + [else + (vector-set! turns curr turn) + (loop (add1 turn) 0)])))) + +(show-solution (play 2020) (play 30000000))