You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
aoc-racket/2020/13.rkt

41 lines
1.3 KiB
Racket

4 years ago
#lang br
4 years ago
(require racket/file rackunit racket/dict)
4 years ago
(match-define (cons target buses-all)
(for/list ([tok
(in-port read
(open-input-string
(string-replace (file->string "13.rktd") "," " ")))])
tok))
(define (overshoot bus)
(for/last ([i (in-naturals)]
#:final (> (* i bus) target))
(- (* i bus) target)))
4 years ago
(define (solve-1)
(define winner (argmin overshoot (filter integer? buses-all)))
(* winner (overshoot winner)))
(check-equal? (solve-1) 246)
(define (solve-2)
(define pairs (for/list ([(b i) (in-indexed buses-all)]
#:when (integer? b))
(cons b i)))
(for/fold ([t 0]
[increment 1]
#:result t)
([(bus overshoot) (in-dict pairs)])
(define mod-target (let loop ([overshoot overshoot])
(cond
[(zero? overshoot) 0]
[(< overshoot bus) (- bus overshoot)]
[else (loop (- overshoot bus))])))
(define multiplier
(for/first ([multiplier (in-naturals)]
#:when (= (modulo (+ t (* increment multiplier)) bus) mod-target))
multiplier))
(values (+ t (* increment multiplier)) (* increment bus))))
4 years ago
4 years ago
(check-equal? (solve-2) 939490236001473)