43 lines
1.2 KiB
Racket
43 lines
1.2 KiB
Racket
#lang curly-fn racket
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
;; solution for day 15
|
|
|
|
(require racket/fixnum racket/unsafe/ops)
|
|
|
|
(define (naive-solution input limit)
|
|
(define seen-numbers (make-fxvector limit -1))
|
|
(define prev
|
|
(for/last ([num (in-list input)] [i (in-naturals)])
|
|
(begin0 num (unsafe-fxvector-set! seen-numbers num i))))
|
|
;; delay "seeing" numbers by one iteration
|
|
(unsafe-fxvector-set! seen-numbers prev -1)
|
|
|
|
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
|
(begin0
|
|
(match (unsafe-fxvector-ref seen-numbers prev)
|
|
[-1 0]
|
|
[idx (unsafe-fx- i 1 idx)])
|
|
;; _now_ we've "seen" it, but it happened on the last turn
|
|
(unsafe-fxvector-set! seen-numbers prev (unsafe-fx- i 1)))))
|
|
|
|
(define part1 #{naive-solution % 2020})
|
|
(define part2 #{naive-solution % 30000000})
|
|
|
|
;; parse input file
|
|
(define (parse fname)
|
|
(define input (string-trim (file->string fname)))
|
|
(map string->number (string-split input ",")))
|
|
|
|
(module+ test
|
|
(require rackunit)
|
|
;; tests here
|
|
(displayln "no tests :("))
|
|
|
|
(module+ main
|
|
(define input (parse "inputs/15"))
|
|
(answer 15 1 (time (part1 input)))
|
|
(answer 15 2 (time (part2 input)))
|
|
(displayln "meow"))
|