summaryrefslogtreecommitdiffstats
path: root/2021/17/code.tl
blob: af76de058c03c7201359961629356e9041b36683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)))