2020-12-15 05:16:18 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 15
|
|
|
|
|
|
|
|
(define (naive-solution input limit)
|
|
|
|
(define seen-numbers (make-hash))
|
2020-12-15 05:27:14 +00:00
|
|
|
(define prev
|
|
|
|
(for/last ([num (in-list input)] [i (in-naturals)])
|
2020-12-15 05:37:12 +00:00
|
|
|
(begin0 num (hash-set! seen-numbers num i))))
|
2020-12-15 05:27:14 +00:00
|
|
|
;; delay "seeing" numbers by one iteration
|
2020-12-15 05:16:18 +00:00
|
|
|
(hash-remove! seen-numbers prev)
|
|
|
|
|
2020-12-15 05:27:14 +00:00
|
|
|
(for/fold ([prev prev]) ([i (in-range (length input) limit)])
|
2020-12-15 05:37:12 +00:00
|
|
|
(begin0
|
2020-12-15 05:27:14 +00:00
|
|
|
(match (hash-ref seen-numbers prev #f)
|
|
|
|
[#f 0]
|
2020-12-15 05:37:12 +00:00
|
|
|
[idx (- i 1 idx)])
|
|
|
|
;; _now_ we've "seen" it, but it happened on the last turn
|
|
|
|
(hash-set! seen-numbers prev (sub1 i)))))
|
2020-12-15 05:16:18 +00:00
|
|
|
|
|
|
|
(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"))
|