aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2020/day-12/day-12.rkt56
-rw-r--r--2020/day-13/day-13.rkt32
-rw-r--r--2022/day-06/day-06.ipynb3
-rw-r--r--2022/day-06/day-06.rkt3
4 files changed, 90 insertions, 4 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)))
diff --git a/2022/day-06/day-06.ipynb b/2022/day-06/day-06.ipynb
index 3468d68..0c89fa1 100644
--- a/2022/day-06/day-06.ipynb
+++ b/2022/day-06/day-06.ipynb
@@ -22,7 +22,6 @@
"source": [
"(require racket\n",
" advent-of-code\n",
- " (only-in relation ->list)\n",
" (only-in algorithms sliding))\n"
]
},
@@ -64,7 +63,7 @@
" (match type\n",
" ['start-of-packet 4]\n",
" ['start-of-message 14]))\n",
- " (for/first ([chunk (in-list (sliding (->list data) n))]\n",
+ " (for/first ([chunk (in-list (sliding (string->list data) n))]\n",
" [i (in-naturals n)]\n",
" #:when (= n (~> chunk remove-duplicates length)))\n",
" i))\n",
diff --git a/2022/day-06/day-06.rkt b/2022/day-06/day-06.rkt
index 89b8075..1c167a6 100644
--- a/2022/day-06/day-06.rkt
+++ b/2022/day-06/day-06.rkt
@@ -1,7 +1,6 @@
#lang racket
(require advent-of-code
- (only-in relation ->list ->set)
(only-in algorithms sliding))
(define buffer (fetch-aoc-input (find-session) 2022 6))
@@ -11,7 +10,7 @@
(case type
[(start-of-packet) 4]
[(start-of-message) 14]))
- (for/first ([chunk (in-list (sliding (->list data) n))]
+ (for/first ([chunk (in-list (sliding (string->list data) n))]
[i (in-naturals n)]
#:unless (check-duplicates chunk))
i))