Skip to content

Commit 044af24

Browse files
committed
day14.clj: 2x faster
Converting the result of `pmap` to `vec` gives huge benefits.
1 parent 7258ba2 commit 044af24

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

clojure/day14.clj

+9-14
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
[clojure.string :as str]))
44

55

6-
(defn rotate [platform]
7-
(->> platform
8-
aoc/transpose
9-
(map str/join)))
10-
6+
(defn transpose [platform]
7+
(apply map str platform))
118

129
(defn move-line [dir line]
1310
(->> (str/split line #"#" -1)
@@ -20,16 +17,16 @@
2017

2118

2219
(defn move-east [platform]
23-
(pmap #(move-line :right %) platform))
20+
(vec (pmap #(move-line :right %) platform)))
2421

2522
(defn move-west [platform]
26-
(pmap #(move-line :left %) platform))
23+
(vec (pmap #(move-line :left %) platform)))
2724

2825
(defn move-north [platform]
29-
(-> platform rotate move-west rotate))
26+
(-> platform transpose move-west transpose))
3027

3128
(defn move-south [platform]
32-
(-> platform rotate move-east rotate))
29+
(-> platform transpose move-east transpose))
3330

3431
(defn spin-cycle [platform]
3532
(-> platform move-north move-west move-south move-east))
@@ -38,11 +35,9 @@
3835
(defn calc-score [platform]
3936
(let [platform (vec platform)
4037
size (count platform)]
41-
(reduce
42-
(fn [acc n]
43-
(+ acc (* (- size n)
44-
(aoc/count-if #{\O} (platform n)))))
45-
0
38+
(aoc/sum-map
39+
(fn [n] (* (- size n)
40+
(aoc/count-if #{\O} (platform n))))
4641
(range size))))
4742

4843

0 commit comments

Comments
 (0)