/
day7.clj
52 lines (39 loc) · 1.24 KB
/
day7.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
42
43
44
45
46
47
48
49
50
51
52
(ns day7)
(def example-input "16,1,2,0,4,2,7,1,2,14")
(def real-input (slurp "input7"))
(def input real-input)
(def initial-positions (->> input (format "[%s]") read-string))
(defn min-cost [cost-fn]
(->> initial-positions
(map (fn [n]
(reduce
(fn [cost pos]
(+ cost (cost-fn (Math/abs (- pos n)))))
0
initial-positions)))
(reduce min)))
;; part1
(time (min-cost identity))
;; "Elapsed time: 2187.234532 msecs"
;; => 357353
;; part2
;; Thank you, 6-year-old Gauss!
(time (min-cost #(/ (* % (inc %)) 2)))
;; "Elapsed time: 2229.310717 msecs"
;; => 104822130
;; slightly optimized; hardly worth the trouble
(def initial-position-freqs (frequencies initial-positions))
(defn min-cost' [cost-fn]
(->> initial-position-freqs
(map (fn [[pos pos-count]]
(* pos-count
(reduce
(fn [cost [pos' pos-count']]
(+ cost (* pos-count' (cost-fn (Math/abs (- pos' pos))))))
0
initial-position-freqs))))
(reduce min)))
(time (min-cost' identity))
;; "Elapsed time: 929.02989 msecs"
(time (min-cost' #(/ (* % (inc %)) 2)))
;; "Elapsed time: 965.869357 msecs"