aboutsummaryrefslogtreecommitdiff
path: root/2020/day-13/day-13.rkt
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-07 14:34:49 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-07 14:34:49 -0500
commit1a9df79bcd45b24476465cca61d8d57bbb94a89a (patch)
tree81bd7441e7c7eaeeb66ae9ab94ff84cbd76fd2b3 /2020/day-13/day-13.rkt
parent1012f0145986ec742519857e0ea810924e3e15e9 (diff)
downloadgleam_aoc-1a9df79bcd45b24476465cca61d8d57bbb94a89a.tar.gz
gleam_aoc-1a9df79bcd45b24476465cca61d8d57bbb94a89a.zip
2020 day 12 and 13 done
Diffstat (limited to '2020/day-13/day-13.rkt')
-rw-r--r--2020/day-13/day-13.rkt32
1 files changed, 32 insertions, 0 deletions
diff --git a/2020/day-13/day-13.rkt b/2020/day-13/day-13.rkt
new file mode 100644
index 0000000..b53f045
--- /dev/null
+++ b/2020/day-13/day-13.rkt
@@ -0,0 +1,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)))