From a1af4e9dec325a3fff03140999ac737c983d1f49 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 7 Dec 2021 14:45:32 -0500 Subject: [PATCH] day7: improve part1; add maxima solutions --- day7/maxima.mx | 10 ++++++++++ day7/part1.clj | 30 +++++++++++++----------------- 2 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 day7/maxima.mx diff --git a/day7/maxima.mx b/day7/maxima.mx new file mode 100644 index 0000000..4625fdc --- /dev/null +++ b/day7/maxima.mx @@ -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))); + diff --git a/day7/part1.clj b/day7/part1.clj index 03c2aa6..f08e5b4 100644 --- a/day7/part1.clj +++ b/day7/part1.clj @@ -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 $) + )