aboutsummaryrefslogtreecommitdiffstats
path: root/day7
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-12-07 08:52:55 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-12-07 08:52:55 -0500
commitec649250aaab079ce396c8358d8e2312e89eed53 (patch)
tree41a41ba463102d1f4cedca076bacf5544c63b65b /day7
parent65df6869e9c1e35222d6ac85bd253ef1577a946e (diff)
day7: improved
Diffstat (limited to 'day7')
-rw-r--r--day7/part1.clj30
-rw-r--r--day7/part2.clj36
2 files changed, 31 insertions, 35 deletions
diff --git a/day7/part1.clj b/day7/part1.clj
index 43426f8..03c2aa6 100644
--- a/day7/part1.clj
+++ b/day7/part1.clj
@@ -1,26 +1,22 @@
-(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)
+ (cond->> (nth srt hlf)
+ (even? cnt)
+ (+ (nth srt (dec hlf)))
+ :true
+ (#(quot % 2))
)
)
)
-(println
- (->> input
- (map (partial - (median input)))
- (map #(if (neg? %) (- %) %))
- (apply +)
- )
- )
+(->> (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)
+ )
diff --git a/day7/part2.clj b/day7/part2.clj
index c051a6a..83d2033 100644
--- a/day7/part2.clj
+++ b/day7/part2.clj
@@ -1,23 +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 %))))
+(defn calc-fuel [lst pos]
+ (->> lst
+ (map
+ (comp
+ #(/ (* % (inc %)) 2)
+ #(Math/abs %)
+ (partial - pos)
+ )
+ )
(apply +)
)
)
-(println (min (calc-fuel input (mean input))
- (calc-fuel input (inc (mean input)))))
+(let [input (->> (slurp "./in") ;"16,1,2,0,4,2,7,1,2,14"
+ (#(clojure.string/split % #","))
+ (map read-string)
+ )
+ mean (quot (apply + input) (count input))
+ ]
+ (println (min (calc-fuel input mean)
+ (calc-fuel input (inc mean))))
+ )