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.

41 lines
1.2 KiB

#lang racket
(require "lib/common.rkt"
math/number-theory)
(provide (all-defined-out))
(define (preprocess prt)
(match-define (list (app string->number tm) ids) (port->lines prt))
(define ids/parsed
(for/list ([id (in-list (string-split ids ","))])
(define num (string->number id))
(if num num 'empty)))
(values tm ids/parsed))
(define (in-bus id)
(in-range 0 +inf.0 id))
(define (day13a tm ids)
(define ids/f (filter (lambda (v) (not (eqv? v 'empty))) ids))
(define lst
(for/list ([id (in-list ids/f)])
(cons id (for/first ([dep (in-bus id)]
#:when (> dep tm))
dep))))
(match-define (cons id dep) (argmin cdr lst))
(* id (- dep tm)))
(define (day13b ids)
(define eqns
(for/fold ([eqns '()])
([id (in-list ids)]
[i (in-naturals)]
#:unless (equal? id 'empty))
(cons (cons (- id i) id) eqns)))
(solve-chinese (map car eqns) (map cdr eqns)))
(module+ main
(call-with-input-file "data/day13.txt"
(lambda (prt)
(define-values (tm ids) (preprocess prt))
(answer 13 1 (day13a tm ids))
(answer 13 2 (day13b ids)))))