day7: improved

master
Clyne 3 years ago
parent 65df6869e9
commit ec649250aa

@ -1,26 +1,22 @@
(def input
(->> (slurp "./in") ; "16,1,2,0,4,2,7,1,2,14"
(#(clojure.string/split % #","))
(map read-string)
)
)
(defn median [lst] (defn median [lst]
(let [cnt (count lst) (let [cnt (count lst)
hlf (quot cnt 2) hlf (quot cnt 2)
srt (sort lst)] srt (sort lst)]
(if (odd? cnt) (cond->> (nth srt hlf)
(nth srt hlf) (even? cnt)
(quot (+ (nth srt hlf) (nth srt (dec hlf))) 2) (+ (nth srt (dec hlf)))
:true
(#(quot % 2))
) )
) )
) )
(println (->> (slurp "./in") ; "16,1,2,0,4,2,7,1,2,14"
(->> input (#(clojure.string/split % #","))
(map (partial - (median input))) (map read-string)
(map #(if (neg? %) (- %) %)) (#(map (partial - (median %)) %))
(apply +) (map #(Math/abs %))
) (apply +)
) (println)
)

@ -1,23 +1,23 @@
(def input (defn calc-fuel [lst pos]
(->> (slurp "./in") ;"16,1,2,0,4,2,7,1,2,14" (->> lst
(#(clojure.string/split % #",")) (map
(map read-string) (comp
) #(/ (* % (inc %)) 2)
) #(Math/abs %)
(partial - pos)
(defn mean [lst] )
(quot (apply + lst) (count lst)) )
)
(defn calc-fuel [lst meen]
(->> input
(map (partial - meen))
(map #(if (neg? %) (- %) %))
(map #(apply + (range 1 (inc %))))
(apply +) (apply +)
) )
) )
(println (min (calc-fuel input (mean input)) (let [input (->> (slurp "./in") ;"16,1,2,0,4,2,7,1,2,14"
(calc-fuel input (inc (mean input))))) (#(clojure.string/split % #","))
(map read-string)
)
mean (quot (apply + input) (count input))
]
(println (min (calc-fuel input mean)
(calc-fuel input (inc mean))))
)

Loading…
Cancel
Save