Advent of Code 2020 solutions in Racket, I guess
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

#lang typed/racket/base
(require racket/list
(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))))))