aboutsummaryrefslogtreecommitdiff
path: root/2021/day7/07a.clj
blob: d9cb0f79668e530145dbe897a2597b88b4d4790d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)))