day7: improve part1; add maxima solutions
parent
803b9b677f
commit
a1af4e9dec
@ -0,0 +1,10 @@
|
|||||||
|
load("descriptive")$
|
||||||
|
batchload("./in.mx")$ /* Should define `input` list of numbers. */
|
||||||
|
|
||||||
|
calcfuel(pos, distmod) :=
|
||||||
|
lreduce("+", map(lambda([n], distmod(abs(pos - n))), input))$
|
||||||
|
|
||||||
|
calcfuel(median(input), lambda([x],x));
|
||||||
|
calcfuel(floor(mean(input)), lambda([x], (x*(x+1)/2)));
|
||||||
|
calcfuel(floor(mean(input)) + 1, lambda([x], (x*(x+1)/2)));
|
||||||
|
|
@ -1,22 +1,18 @@
|
|||||||
(defn median [lst]
|
(defn median [lst]
|
||||||
(let [cnt (count lst)
|
(as-> (count lst) $
|
||||||
hlf (quot cnt 2)
|
(quot $ 2)
|
||||||
srt (sort lst)]
|
(subvec (vec (sort lst)) (dec $) (inc $))
|
||||||
(cond->> (nth srt hlf)
|
(if (even? (count lst)) (apply + $) (second $))
|
||||||
(even? cnt)
|
(quot $ 2)
|
||||||
(+ (nth srt (dec hlf)))
|
|
||||||
:true
|
|
||||||
(#(quot % 2))
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
(->> (slurp "./in") ; "16,1,2,0,4,2,7,1,2,14"
|
(as-> (slurp "./in") $ ; "16,1,2,0,4,2,7,1,2,14"
|
||||||
(#(clojure.string/split % #","))
|
(clojure.string/split $ #",")
|
||||||
(map read-string)
|
(mapv read-string $)
|
||||||
(#(map (partial - (median %)) %))
|
(map (partial - (median $)) $)
|
||||||
(map #(Math/abs %))
|
(map #(Math/abs %) $)
|
||||||
(apply +)
|
(apply + $)
|
||||||
(println)
|
(println $)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue