aboutsummaryrefslogtreecommitdiff
path: root/racket/aoc2020/day-13/day-13.rkt
blob: b53f045a54b3cd7338c8a67269cfff4bd881fa40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#lang racket

(require advent-of-code
         (only-in relation ->number)
         threading)

(define (process-ids str)
  (~> str (string-split ",") (filter-map (λ (s) (string->number s 10 'number-or-false)) _)))

(match-define (regexp #px"(\\d+)\n(.+)" (list _ (app ->number timestamp) raw-bus-ids))
  (fetch-aoc-input (find-session) 2020 13))

(define bus-ids (process-ids raw-bus-ids))

;; part 1
(for/first ([minute (in-naturals timestamp)]
            #:do [(define departing-bus
                    (for/first ([b bus-ids] #:when (= 0 (remainder minute b)))
                      b))]
            #:when departing-bus)
  (* departing-bus (- minute timestamp)))

;; part 2
(for/fold ([step 1] [current-timestamp 1] #:result current-timestamp)
          ([b* (in-list (string-split (string-trim raw-bus-ids) ","))]
           [offset (in-naturals)]
           #:unless (equal? b* "x")
           #:do [(define bus (->number b*))])
  (values
   (* step bus)
   (for/first ([n (in-range current-timestamp +inf.0 step)] #:when (= 0 (remainder (+ n offset) bus)))
     n)))