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.
55 lines
1.8 KiB
Clojure
55 lines
1.8 KiB
Clojure
3 years ago
|
(require '[clojure.core.reducers :as r])
|
||
|
|
||
|
;(def target-area [[169 -68] [206 -108]])
|
||
|
(def target-area [[20000 -5000] [30000 -10000]])
|
||
|
;(def target-area [[2000 -500] [3000 -1000]])
|
||
3 years ago
|
(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]]]
|
||
3 years ago
|
[[(+ px vx) (+ py vy)] [(cond-> vx (pos? vx) dec) (dec vy)]])
|
||
3 years ago
|
|
||
3 years ago
|
(defn take-last-while [pred coll]
|
||
|
(loop [v (first coll) r (rest coll)]
|
||
|
(if (not (pred (first r)))
|
||
|
v
|
||
|
(recur (first r) (rest r)))))
|
||
|
|
||
3 years ago
|
(defn build-path [target vel]
|
||
3 years ago
|
(->> (iterate apply-velocity [initial-pos vel])
|
||
3 years ago
|
(take-last-while (comp not (partial beyond-target? target) first))
|
||
|
(first)))
|
||
3 years ago
|
|
||
|
; 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)))
|
||
3 years ago
|
highest-y (dec (Math/abs (second te)))]
|
||
3 years ago
|
(->> (for [y (range (second te) (inc highest-y))
|
||
|
x (range lowest-x (inc (first te)))] [x y])
|
||
|
(#(do (println "Generated xy pairs...") %))
|
||
|
(#(do (println "Total: " (* (- highest-y (second te)) (- (inc highest-y) lowest-x))) %))
|
||
|
(partition 50000)
|
||
|
(#(do (println "Prepared partitions...") %))
|
||
|
(reduce
|
||
|
(fn [sum nlst]
|
||
|
(println sum)
|
||
|
(+ sum
|
||
|
(r/fold +
|
||
|
(r/monoid
|
||
|
(fn [tot xy]
|
||
|
(cond-> tot
|
||
|
(within-target? target-area (build-path target-area xy))
|
||
|
inc))
|
||
|
(constantly 0))
|
||
|
(into [] nlst))))
|
||
|
0)
|
||
|
(println)))
|
||
3 years ago
|
|