summaryrefslogtreecommitdiffstats
path: root/2021/17/code.tl
diff options
context:
space:
mode:
Diffstat (limited to '2021/17/code.tl')
-rw-r--r--2021/17/code.tl46
1 files changed, 46 insertions, 0 deletions
diff --git a/2021/17/code.tl b/2021/17/code.tl
new file mode 100644
index 0000000..af76de0
--- /dev/null
+++ b/2021/17/code.tl
@@ -0,0 +1,46 @@
+(defstruct spec ()
+ x0 x1
+ y0 y1)
+
+(defun read-input (: (name "input"))
+ (flow name
+ file-get-string
+ (match `target area: x=@x0..@x1, y=@y0..@y1\n@nil` @1
+ (new spec x0 (toint x0) x1 (toint x1) y0 (toint y0) y1 (toint y1)))))
+
+(defun simulate-shot (sp xv yv)
+ (let ((x0 sp.x0) (x1 sp.x1)
+ (y0 sp.y0) (y1 sp.y1))
+ (for ((x 0) (y 0) (maxy -1000)) () ()
+ (cond
+ ((or (< y y0) (> x x1))
+ (return))
+ ((and (<= x0 x x1) (<= y0 y y1))
+ (return maxy)))
+ (inc x xv)
+ (inc y yv)
+ (upd maxy (max y maxy))
+ (dec xv (signum xv))
+ (dec yv 1))))
+
+(defun solve-one (: (name :))
+ (let ((sp (read-input name)))
+ (flow
+ (build
+ (each-prod ((xv 1..40)
+ (yv 1..700))
+ (iflet ((maxy (simulate-shot sp xv yv)))
+ (add (list xv yv maxy)))))
+ (find-max @1 : caddr))))
+
+(defun solve-two (: (name :))
+ (let ((sp (read-input name)))
+ (flow
+ (build
+ (each-prod ((xv 1..300)
+ (yv -100..400))
+ (iflet ((maxy (simulate-shot sp xv yv)))
+ (add (list xv yv maxy)))))
+ (mapcar (ap list @1 @2))
+ uniq
+ len)))