day7: improve part1; add maxima solutions

master
Clyne 3 years ago
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]
(let [cnt (count lst)
hlf (quot cnt 2)
srt (sort lst)]
(cond->> (nth srt hlf)
(even? cnt)
(+ (nth srt (dec hlf)))
:true
(#(quot % 2))
)
(as-> (count lst) $
(quot $ 2)
(subvec (vec (sort lst)) (dec $) (inc $))
(if (even? (count lst)) (apply + $) (second $))
(quot $ 2)
)
)
(->> (slurp "./in") ; "16,1,2,0,4,2,7,1,2,14"
(#(clojure.string/split % #","))
(map read-string)
(#(map (partial - (median %)) %))
(map #(Math/abs %))
(apply +)
(println)
(as-> (slurp "./in") $ ; "16,1,2,0,4,2,7,1,2,14"
(clojure.string/split $ #",")
(mapv read-string $)
(map (partial - (median $)) $)
(map #(Math/abs %) $)
(apply + $)
(println $)
)

Loading…
Cancel
Save