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

9 months ago
  1. #lang typed/racket/base
  2. (require racket/list
  3. racket/port
  4. racket/string)
  5. (require/typed "lib/common.rkt"
  6. [answer (-> Positive-Integer Positive-Integer Integer Void)])
  7. (: preprocess (-> Input-Port (Listof Integer)))
  8. (define (preprocess prt)
  9. (cast (map string->number (string-split (string-trim (port->string prt)) ","))
  10. (Listof Integer)))
  11. (: solution (-> (Listof Integer) Integer Integer))
  12. (define (solution starters end)
  13. (define turns : (Mutable-Vectorof (U Integer False))
  14. (make-vector end #f))
  15. (for ([strt (in-list starters)]
  16. [turn (in-range 1 (add1 (length starters)))])
  17. (vector-set! turns strt turn))
  18. (for/fold ([curr (last starters)])
  19. ([turn (in-range (length starters) end)])
  20. (define val (vector-ref turns curr))
  21. (vector-set! turns curr turn)
  22. (if val (- turn val) 0)))
  23. (: day15a (-> (Listof Integer) Integer))
  24. (define (day15a input)
  25. (solution input 2020))
  26. (: day15b (-> (Listof Integer) Integer))
  27. (define (day15b input)
  28. (solution input 30000000))
  29. (module+ main
  30. (call-with-input-file "data/day15.txt"
  31. (lambda ([prt : Input-Port])
  32. (define input (preprocess prt))
  33. (answer 15 1 (time (day15a input)))
  34. (answer 15 2 (time (day15b input))))))