]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day7: improve part1; add maxima solutions
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 7 Dec 2021 19:45:32 +0000 (14:45 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 7 Dec 2021 19:45:32 +0000 (14:45 -0500)
day7/maxima.mx [new file with mode: 0644]
day7/part1.clj

diff --git a/day7/maxima.mx b/day7/maxima.mx
new file mode 100644 (file)
index 0000000..4625fdc
--- /dev/null
@@ -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)));
+
index 03c2aa655208b7afa8db78d9bd8a062fccfa05f0..f08e5b4054b08d434a98dd68e57b09e597144418 100644 (file)
@@ -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 $)
+      )