aboutsummaryrefslogtreecommitdiff
path: root/2021/day7
diff options
context:
space:
mode:
authorAria <me@aria.rip>2023-01-02 21:58:56 +0000
committerAria <me@aria.rip>2023-01-02 21:58:56 +0000
commit5eb58ad076f2cd435b11b140820da224b60b73d5 (patch)
tree2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2021/day7
initial commit
Diffstat (limited to '2021/day7')
-rw-r--r--2021/day7/07a.clj19
-rw-r--r--2021/day7/07b.clj22
2 files changed, 41 insertions, 0 deletions
diff --git a/2021/day7/07a.clj b/2021/day7/07a.clj
new file mode 100644
index 0000000..d9cb0f7
--- /dev/null
+++ b/2021/day7/07a.clj
@@ -0,0 +1,19 @@
+(ns day-7)
+
+(require '[clojure.string :as str])
+
+(defn totalDelta [xs dst]
+ (reduce + (map (fn [x] (Math/abs ^int (- x dst))) xs)))
+
+(defn calcIdealPos [xs start]
+ (def thisDelta (totalDelta xs start))
+ (def nextDelta (totalDelta xs (+ 1 start)))
+ (cond (> nextDelta thisDelta) start
+ :else (calcIdealDst xs (+ 1 start))))
+
+(def input (as-> (slurp "./input") x
+ (str/split x #",")
+ (map str/trim x)
+ (map #(. Integer parseInt %) x)))
+
+(totalDelta input (calcIdealPos input (apply min input)))
diff --git a/2021/day7/07b.clj b/2021/day7/07b.clj
new file mode 100644
index 0000000..9e248c8
--- /dev/null
+++ b/2021/day7/07b.clj
@@ -0,0 +1,22 @@
+(ns day-7)
+
+(require '[clojure.string :as str])
+
+(defn fuelCost [x]
+ (/ (* x (+ 1 x)) 2))
+
+(defn totalDelta [xs dst]
+ (reduce + (map (fn [x] (fuelCost (Math/abs ^int (- x dst)))) xs)))
+
+(defn calcIdealPos [xs start]
+ (def thisDelta (totalDelta xs start))
+ (def nextDelta (totalDelta xs (+ 1 start)))
+ (cond (> nextDelta thisDelta) start
+ :else (calcIdealDst xs (+ 1 start))))
+
+(def input (as-> (slurp "./input") x
+ (str/split x #",")
+ (map str/trim x)
+ (map #(. Integer parseInt %) x)))
+
+(totalDelta input (calcIdealPos input (apply min input)))