From d6124e1dd1e2f11c081daf991b6000d5a1666917 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Tue, 8 Sep 2020 09:31:16 -0700 Subject: 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. --- challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj | 8 ++++---- 1 file 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 -- cgit