aoc2020/13.rkt

43 lines
1.2 KiB
Racket

#lang curly-fn racket
(require "scripts/aoc.rkt")
(require math/number-theory)
;; solution for day 13
(define (part1 input)
(match-define (cons time buses) input)
(define (find-bus x)
(for/first ([bus (in-list buses)] #:unless (equal? 'x bus) #:when (zero? (modulo x bus)))
bus))
(for*/first ([x (in-range time +inf.0)] [bus (in-value (find-bus x))] #:when bus)
(* bus (- x time))))
(define (part2 input)
(match-define (cons _ buses) input)
(define eqns
(for/fold ([eqns '()]) ([bus (in-list buses)] [i (in-naturals)] #:unless (equal? bus 'x))
(cons (cons (- bus i) bus) eqns)))
(solve-chinese (map car eqns) (map cdr eqns)))
;; parse input file
(define (parse fname)
(define input (file->lines fname))
(define time (string->number (first input)))
(define buses (for/list ([x (in-list (string-split (second input) ","))])
(match x
["x" 'x]
[_ (string->number x)])))
(cons time buses))
(module+ test
(require rackunit)
;; tests here
(displayln "no tests :("))
(module+ main
(define input (parse "inputs/13"))
(answer 13 1 (time (part1 input)))
(answer 13 2 (time (part2 input)))
(displayln "meow"))