day18: simplify part 2 'for'

master
Clyne 3 years ago
parent 0755910e65
commit 9ef614f9bc

@ -1,5 +1,8 @@
(def target-area [[169 -68] [206 -108]]) (require '[clojure.core.reducers :as r])
;(def target-area [[20 -5] [30 -10]])
;(def target-area [[169 -68] [206 -108]])
(def target-area [[20000 -5000] [30000 -10000]])
;(def target-area [[2000 -500] [3000 -1000]])
(def initial-pos [0 0]) (def initial-pos [0 0])
(defn beyond-target? [[[tbx tby] [tex tey]] [x y]] (defn beyond-target? [[[tbx tby] [tex tey]] [x y]]
@ -11,15 +14,16 @@
(defn apply-velocity [[[px py] [vx vy]]] (defn apply-velocity [[[px py] [vx vy]]]
[[(+ px vx) (+ py vy)] [(cond-> vx (pos? vx) dec) (dec vy)]]) [[(+ px vx) (+ py vy)] [(cond-> vx (pos? vx) dec) (dec vy)]])
(defn take-last-while [pred coll]
(loop [v (first coll) r (rest coll)]
(if (not (pred (first r)))
v
(recur (first r) (rest r)))))
(defn build-path [target vel] (defn build-path [target vel]
(->> (iterate apply-velocity [initial-pos vel]) (->> (iterate apply-velocity [initial-pos vel])
(take-while (comp not (partial beyond-target? target) first)) (take-last-while (comp not (partial beyond-target? target) first))
(mapv first))) (first)))
(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 ; Used to determine best x velocity for highest path
(def tnum-seq (iterate #(do [(apply + %) (inc (second %))]) [0 1])) (def tnum-seq (iterate #(do [(apply + %) (inc (second %))]) [0 1]))
@ -27,11 +31,24 @@
(let [[tb te] target-area (let [[tb te] target-area
lowest-x (second (last (take-while #(< (first %) (first tb)) tnum-seq))) lowest-x (second (last (take-while #(< (first %) (first tb)) tnum-seq)))
highest-y (dec (Math/abs (second te)))] highest-y (dec (Math/abs (second te)))]
(println (->> (for [y (range (second te) (inc highest-y))
(path-height target-area [lowest-x highest-y]) x (range lowest-x (inc (first te)))] [x y])
(count (#(do (println "Generated xy pairs...") %))
(filter (partial within-target? target-area) (#(do (println "Total: " (* (- highest-y (second te)) (- (inc highest-y) lowest-x))) %))
(for [x (range lowest-x (inc (first te))) (partition 50000)
y (range (second te) (inc highest-y))] (#(do (println "Prepared partitions...") %))
(last (build-path target-area [x y]))))))) (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)))

@ -87,10 +87,8 @@
(println (snailfish-solve input)) (println (snailfish-solve input))
; Part 2 ; Part 2
(->> (let [input-set (set input)] (->> (for [in input in2 input :when (not= in in2)] [in in2])
(for [in input in2 (set/difference input-set (set [in]))] (vec)
[in in2]))
(into [])
(r/map snailfish-solve) (r/map snailfish-solve)
(r/fold (r/monoid max (constantly 0))) (r/fold (r/monoid max (constantly 0)))
(println)) (println))

Loading…
Cancel
Save