-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday_07.clj
executable file
·41 lines (30 loc) · 1017 Bytes
/
day_07.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env bb
(require '[clojure.string :as str])
(def input (slurp "./input/day_07.txt"))
;; ------------------------------------ part 1
(defn str->int
[s]
(Integer/parseInt s))
(defn counter
[size]
(zipmap (take size (range)) (repeat size 0)))
(defn distance
[i j]
(let [diff (- i j)]
(max diff (- diff))))
(defn cheapest-fuel
[input & {:keys [rate] :or {rate identity}}]
(let [positions (vec (map str->int (str/split input #",")))
crabs-num (count positions)
find-fuel #(apply min (vals % ))]
(find-fuel (reduce (fn [dicto [i j]]
(update dicto i #(+ % (rate (distance (positions j) i)))))
(counter crabs-num)
(for [i (range crabs-num) j (range crabs-num)]
[i j])))))
(println "sol 1:" (cheapest-fuel input))
;; ------------------------------------ part 2
(defn nth-triangle
[n]
(/ (* n (inc n)) 2))
(println "sol 2:" (cheapest-fuel input :rate nth-triangle))