]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day7: learn as->
authorClyne Sullivan <clyne@bitgloo.com>
Tue, 7 Dec 2021 14:24:24 +0000 (09:24 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Tue, 7 Dec 2021 14:24:24 +0000 (09:24 -0500)
day7/part2.clj

index 83d20333271cfbb6fa88a369c8fffeb8a0ec1b1e..b96b55da3811af008a7792af5e008d0e278c39b6 100644 (file)
@@ -1,22 +1,19 @@
 (defn calc-fuel [lst pos]
-  (->> lst
-       (map
-         (comp
-           #(/ (* % (inc %)) 2)
-           #(Math/abs %)
-           (partial - pos)
-           )
-         )
-       (apply +)
-       )
+  (reduce #(as-> %2 $
+             (- pos $)
+             (Math/abs $)
+             (/ (* $ (inc $)) 2)
+             (+ %1 $)
+             )
+          0 lst
+          )
   )
 
-(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))
-      ]
+(let [input (as-> (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))))
   )