]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
add day 7
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 7 Dec 2021 12:48:34 +0000 (07:48 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 7 Dec 2021 12:48:34 +0000 (07:48 -0500)
day7/part1.clj [new file with mode: 0644]
day7/part2.clj [new file with mode: 0644]

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