Browse Source

typed racket 15

canon
Hazel Levine 7 months ago
parent
commit
255140279f
Signed by: hazel GPG Key ID: 1884029A28789A62
  1. 1
      data/day15.txt
  2. 39
      day15.rkt

1
data/day15.txt

@ -0,0 +1 @@
2,0,6,12,1,3

39
day15.rkt

@ -0,0 +1,39 @@
#lang typed/racket/base
(require racket/list
racket/port
racket/string)
(require/typed "lib/common.rkt"
[answer (-> Positive-Integer Positive-Integer Integer Void)])
(: preprocess (-> Input-Port (Listof Integer)))
(define (preprocess prt)
(cast (map string->number (string-split (string-trim (port->string prt)) ","))
(Listof Integer)))
(: solution (-> (Listof Integer) Integer Integer))
(define (solution starters end)
(define turns : (Mutable-Vectorof (U Integer False))
(make-vector end #f))
(for ([strt (in-list starters)]
[turn (in-range 1 (add1 (length starters)))])
(vector-set! turns strt turn))
(for/fold ([curr (last starters)])
([turn (in-range (length starters) end)])
(define val (vector-ref turns curr))
(vector-set! turns curr turn)
(if val (- turn val) 0)))
(: day15a (-> (Listof Integer) Integer))
(define (day15a input)
(solution input 2020))
(: day15b (-> (Listof Integer) Integer))
(define (day15b input)
(solution input 30000000))
(module+ main
(call-with-input-file "data/day15.txt"
(lambda ([prt : Input-Port])
(define input (preprocess prt))
(answer 15 1 (time (day15a input)))
(answer 15 2 (time (day15b input))))))
Loading…
Cancel
Save