diff --git a/day7/part1.clj b/day7/part1.clj new file mode 100644 index 0000000..43426f8 --- /dev/null +++ b/day7/part1.clj @@ -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 +) + ) + ) + diff --git a/day7/part2.clj b/day7/part2.clj new file mode 100644 index 0000000..c051a6a --- /dev/null +++ b/day7/part2.clj @@ -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))))) +