aboutsummaryrefslogtreecommitdiff
path: root/2020
diff options
context:
space:
mode:
Diffstat (limited to '2020')
-rw-r--r--2020/day-12/day-12.rkt56
-rw-r--r--2020/day-13/day-13.rkt32
2 files changed, 88 insertions, 0 deletions
diff --git a/2020/day-12/day-12.rkt b/2020/day-12/day-12.rkt
new file mode 100644
index 0000000..e4bbd32
--- /dev/null
+++ b/2020/day-12/day-12.rkt
@@ -0,0 +1,56 @@
+#lang rackjure
+
+(require advent-of-code
+ threading
+ (only-in relation ->symbol ->number))
+
+(struct instruction (direction distance) #:transparent)
+
+(define (parse-instruction str)
+ (match str
+ [(regexp #px"(\\w)(\\d+)" (list _ dir dist)) (instruction (->symbol dir) (->number dist))]))
+
+(define instructions
+ (~>> (fetch-aoc-input (find-session) 2020 12) string-split (map parse-instruction)))
+
+;; part 1
+(struct boat (x y nav) #:transparent)
+
+(define (angle->direction n)
+ (case n
+ [(0) 'E]
+ [(90) 'N]
+ [(180) 'W]
+ [(270) 'S]))
+
+(define (move-via-direct-command inst b)
+ (match-define (boat x y facing) b)
+ (match inst
+ [(instruction 'N n) (boat x (+ y n) facing)]
+ [(instruction 'S n) (boat x (- y n) facing)]
+ [(instruction 'E n) (boat (+ x n) y facing)]
+ [(instruction 'W n) (boat (- x n) y facing)]
+ [(instruction 'L n) (boat x y (modulo (+ facing n) 360))]
+ [(instruction 'R n) (boat x y (modulo (- facing n) 360))]
+ [(instruction 'F n) (move-via-direct-command (instruction (angle->direction facing) n) b)]))
+
+(define (find-boat-destination using start instructions)
+ (match-define (boat x y _) (foldl using start instructions))
+ (+ (abs x) (abs y)))
+
+(find-boat-destination move-via-direct-command (boat 0 0 0) instructions)
+
+;; part 2
+(define (move-via-waypoint inst b)
+ (match-define (boat x y (cons wp-x wp-y)) b)
+ (match inst
+ [(instruction 'N n) (boat x y (cons wp-x (+ wp-y n)))]
+ [(instruction 'S n) (boat x y (cons wp-x (- wp-y n)))]
+ [(instruction 'E n) (boat x y (cons (+ wp-x n) wp-y))]
+ [(instruction 'W n) (boat x y (cons (- wp-x n) wp-y))]
+ [(instruction _ 180) (boat x y (cons (- wp-x) (- wp-y)))]
+ [(or (instruction 'L 90) (instruction 'R 270)) (boat x y (cons (- wp-y) wp-x))]
+ [(or (instruction 'R 90) (instruction 'L 270)) (boat x y (cons wp-y (- wp-x)))]
+ [(instruction 'F n) (boat (+ x (* n wp-x)) (+ y (* n wp-y)) (cons wp-x wp-y))]))
+
+(find-boat-destination move-via-waypoint (boat 0 0 '(10 . 1)) instructions)
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)))