add day 7
parent
945a0a01e0
commit
65df6869e9
@ -0,0 +1,26 @@
|
||||
(def input
|
||||
(->> (slurp "./in") ; "16,1,2,0,4,2,7,1,2,14"
|
||||
(#(clojure.string/split % #","))
|
||||
(map read-string)
|
||||
)
|
||||
)
|
||||
|
||||
(defn median [lst]
|
||||
(let [cnt (count lst)
|
||||
hlf (quot cnt 2)
|
||||
srt (sort lst)]
|
||||
(if (odd? cnt)
|
||||
(nth srt hlf)
|
||||
(quot (+ (nth srt hlf) (nth srt (dec hlf))) 2)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
(println
|
||||
(->> input
|
||||
(map (partial - (median input)))
|
||||
(map #(if (neg? %) (- %) %))
|
||||
(apply +)
|
||||
)
|
||||
)
|
||||
|
@ -0,0 +1,23 @@
|
||||
(def input
|
||||
(->> (slurp "./in") ;"16,1,2,0,4,2,7,1,2,14"
|
||||
(#(clojure.string/split % #","))
|
||||
(map read-string)
|
||||
)
|
||||
)
|
||||
|
||||
(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 +)
|
||||
)
|
||||
)
|
||||
|
||||
(println (min (calc-fuel input (mean input))
|
||||
(calc-fuel input (inc (mean input)))))
|
||||
|
Loading…
Reference in New Issue