aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-08 09:31:16 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-08 09:31:16 -0700
commitd6124e1dd1e2f11c081daf991b6000d5a1666917 (patch)
treece33d482cc3fe22e05312ea050b4a3ad221154e3
parentd9bd8a19492ee4633d39ca533c05511b6925aa16 (diff)
downloadperlweeklychallenge-club-d6124e1dd1e2f11c081daf991b6000d5a1666917.tar.gz
perlweeklychallenge-club-d6124e1dd1e2f11c081daf991b6000d5a1666917.tar.bz2
perlweeklychallenge-club-d6124e1dd1e2f11c081daf991b6000d5a1666917.zip
Task 2: improve lonely detection
First, restrict search to elements that are X (or 1 in our case), so we're not doing unnecessary work. Second, we can sum all the elements in the submatrix and if they are equal to 1, then the only 1 is the element we're checking and it is indeed surrounded by all 0s.
-rw-r--r--challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj8
1 files changed, 4 insertions, 4 deletions
diff --git a/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj
index 6c6854eef1..867c33b3fc 100644
--- a/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj
+++ b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj
@@ -24,18 +24,18 @@
(let [n (mat/dimension-count matrix 1)
blank-row (vec (repeat (+ n 2) 0))
step-1 (reduce (fn [mat v] (conj mat (vec (concat [0] v [0])))) [blank-row] matrix)]
- (conj step-1 blank-row)))
+ (to-array-2d (conj step-1 blank-row))))
(defn find-lonelies
"Find lonely values in a matrix."
[matrix]
(let [[m n] (mat/shape matrix)
- extended (extend-matrix matrix)
- canonical-lonely [[0 0 0] [0 1 0] [0 0 0]]]
+ extended (extend-matrix matrix)]
(for [x (range 1 (inc m))
y (range 1 (inc n))
+ :when (= 1 (aget extended x y))
:let [submat (mat/submatrix extended [[(- x 1) 3] [(- y 1) 3]])]
- :when (mat/e= canonical-lonely submat)]
+ :when (= 1 (mat/esum submat))]
[x y])))
(defn -main