You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB
Clojure

3 years ago
(def target-area [[169 -68] [206 -108]])
;(def target-area [[20 -5] [30 -10]])
(def initial-pos [0 0])
(defn beyond-target? [[[tbx tby] [tex tey]] [x y]]
(or (> x tex) (< y tey)))
(defn within-target? [[[tbx tby] [tex tey]] [x y]]
(and (>= x tbx) (<= x tex) (<= y tby) (>= y tey)))
(defn apply-velocity [[[px py] [vx vy]]]
[[(+ px vx) (+ py vy)] [(cond-> vx (pos? vx) dec) (dec vy)]])
3 years ago
(defn build-path [target vel]
(->> (iterate apply-velocity [initial-pos vel])
3 years ago
(take-while (comp not (partial beyond-target? target) first))
(mapv first)))
3 years ago
(defn path-height [target vel]
(let [path (build-path target vel)]
(when (within-target? target (last path))
(apply max (map second path)))))
; Used to determine best x velocity for highest path
(def tnum-seq (iterate #(do [(apply + %) (inc (second %))]) [0 1]))
(let [[tb te] target-area
lowest-x (second (last (take-while #(< (first %) (first tb)) tnum-seq)))
highest-y (dec (Math/abs (second te)))]
3 years ago
(println
(path-height target-area [lowest-x highest-y])
3 years ago
(count
(filter (partial within-target? target-area)
(for [x (range lowest-x (inc (first te)))
y (range (second te) (inc highest-y))]
(last (build-path target-area [x y])))))))
3 years ago