aboutsummaryrefslogtreecommitdiff
path: root/2020/day-13
diff options
context:
space:
mode:
authorHunky Jimpjorps <thechairman@thechairman.info>2022-12-08 09:08:59 -0500
committerHunky Jimpjorps <thechairman@thechairman.info>2022-12-08 09:08:59 -0500
commit7cbae6c649ab735070d05badcf6d29cc1b7607c4 (patch)
treee14c68af9246be78560823e9c26f027ec2c48754 /2020/day-13
parent6e9435ded904c0afcf93f3f324f58e76c214fe0f (diff)
parent0e12a59e64a6f95c2448cc5ddec96984d6111613 (diff)
downloadgleam_aoc-7cbae6c649ab735070d05badcf6d29cc1b7607c4.tar.gz
gleam_aoc-7cbae6c649ab735070d05badcf6d29cc1b7607c4.zip
Merge branch 'main' of https://github.com/hunkyjimpjorps/AdventOfCode into main
Diffstat (limited to '2020/day-13')
-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)))