aboutsummaryrefslogtreecommitdiff
path: root/2021/day-17
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2021-12-17 12:46:45 -0500
committerHJ <thechairman@thechairman.info>2021-12-17 12:46:45 -0500
commitc6655bb85795b2ca51c533e69061d4bfdca19ad6 (patch)
tree13b4a32af653b36e819a4ee3b6424ae2ee32d1a0 /2021/day-17
parentefe6bbfc8500b420ce2485420d5db6e8abd99534 (diff)
downloadgleam_aoc-c6655bb85795b2ca51c533e69061d4bfdca19ad6.tar.gz
gleam_aoc-c6655bb85795b2ca51c533e69061d4bfdca19ad6.zip
day 17 complete
Diffstat (limited to '2021/day-17')
-rw-r--r--2021/day-17/day-17.rkt53
1 files changed, 53 insertions, 0 deletions
diff --git a/2021/day-17/day-17.rkt b/2021/day-17/day-17.rkt
new file mode 100644
index 0000000..b8563a5
--- /dev/null
+++ b/2021/day-17/day-17.rkt
@@ -0,0 +1,53 @@
+#lang racket
+(require "../../jj-aoc.rkt"
+ threading)
+
+(define-values (x-min x-max y-min y-max)
+ (~> (open-day 17 2021)
+ port->string
+ (regexp-match #px"target area: x=(.*)\\.\\.(.*), y=(.*)\\.\\.(.*)\n" _)
+ rest
+ (map string->number _)
+ (apply values _)))
+
+(define (hit? x y)
+ (and (x . >= . x-min)
+ (x . <= . x-max)
+ (y . >= . y-min)
+ (y . <= . y-max)))
+
+(define (miss? x y)
+ (or (y . < . y-min)
+ (x . > . x-max)))
+
+(define (drag dx i) (max (- dx i) 0))
+(define (gravity dy i) (- dy i))
+
+(define (find-trajectory-apex dx dy)
+ (for/fold ([x 0] [y 0] [y-apex 0] [result #f]
+ #:result (list y-apex result))
+ ([i (in-naturals)]
+ #:break result)
+ (cond
+ [(hit? x y) (values dx dy y-apex 'hit)]
+ [(miss? x y) (values x y 'miss 'miss)]
+ [else
+ (values (+ x (drag dx i))
+ (+ y (gravity dy i))
+ (if (y . > . y-apex) y y-apex)
+ #f)])))
+
+(define on-target
+ (for*/list ([dx (in-inclusive-range 1 x-max)]
+ [dy (in-inclusive-range y-min (abs (* 2 y-max)))]
+ [velocity (in-value (find-trajectory-apex dx dy))]
+ #:when (equal? (second velocity) 'hit))
+ (list dx dy (first velocity))))
+
+;; part 1
+(~>> on-target
+ (argmax third)
+ third)
+
+;; part 2
+(length on-target) \ No newline at end of file