From ec649250aaab079ce396c8358d8e2312e89eed53 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 7 Dec 2021 08:52:55 -0500 Subject: day7: improved --- day7/part1.clj | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) (limited to 'day7/part1.clj') diff --git a/day7/part1.clj b/day7/part1.clj index 43426f8..03c2aa6 100644 --- a/day7/part1.clj +++ b/day7/part1.clj @@ -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] (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) + (cond->> (nth srt hlf) + (even? cnt) + (+ (nth srt (dec hlf))) + :true + (#(quot % 2)) ) ) ) -(println - (->> input - (map (partial - (median input))) - (map #(if (neg? %) (- %) %)) - (apply +) - ) - ) +(->> (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) + ) -- cgit v1.2.3