aboutsummaryrefslogtreecommitdiff
path: root/aoc2020/day-13
diff options
context:
space:
mode:
authorJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
committerJ.J <thechairman@thechairman.info>2023-11-30 17:10:00 -0500
commit8ab65dc2da1742eb86ec636c50c7018385b68167 (patch)
treec4fd556aca9b867cfa1f2f174128c30857353884 /aoc2020/day-13
parentfafbeaf9e3c09ba7a5bea7e47d5736001f8a5aa1 (diff)
downloadgleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.tar.gz
gleam_aoc-8ab65dc2da1742eb86ec636c50c7018385b68167.zip
prep for 2023, renaming for consistency
Diffstat (limited to 'aoc2020/day-13')
-rw-r--r--aoc2020/day-13/day-13.rkt32
1 files changed, 32 insertions, 0 deletions
diff --git a/aoc2020/day-13/day-13.rkt b/aoc2020/day-13/day-13.rkt
new file mode 100644
index 0000000..b53f045
--- /dev/null
+++ b/aoc2020/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)))