aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-07 17:36:17 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-09-08 09:12:01 -0700
commit5a97c3c87271a33816ead8373caf8d0856f82d17 (patch)
tree84cb82a922f82007be957c13a92caff82045d8f8
parent6897242ba14084a2b9dfec62122aff71c90015c8 (diff)
downloadperlweeklychallenge-club-5a97c3c87271a33816ead8373caf8d0856f82d17.tar.gz
perlweeklychallenge-club-5a97c3c87271a33816ead8373caf8d0856f82d17.tar.bz2
perlweeklychallenge-club-5a97c3c87271a33816ead8373caf8d0856f82d17.zip
Task 2
-rw-r--r--challenge-077/tyler-wardhaugh/clojure/deps.edn3
-rw-r--r--challenge-077/tyler-wardhaugh/clojure/resources/matrix1.txt3
-rw-r--r--challenge-077/tyler-wardhaugh/clojure/resources/matrix2.txt4
l---------challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj1
-rw-r--r--challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj50
-rw-r--r--challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj8
6 files changed, 66 insertions, 3 deletions
diff --git a/challenge-077/tyler-wardhaugh/clojure/deps.edn b/challenge-077/tyler-wardhaugh/clojure/deps.edn
index b5563f1d16..0d1fedb8bb 100644
--- a/challenge-077/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-077/tyler-wardhaugh/clojure/deps.edn
@@ -1,7 +1,8 @@
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/math.combinatorics {:mvn/version "0.1.6"}
- net.mikera/core.matrix {:mvn/version "0.62.0"}}
+ net.mikera/core.matrix {:mvn/version "0.62.0"}
+ pjstadig/reducible-stream {:mvn/version "0.1.5"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}}
diff --git a/challenge-077/tyler-wardhaugh/clojure/resources/matrix1.txt b/challenge-077/tyler-wardhaugh/clojure/resources/matrix1.txt
new file mode 100644
index 0000000000..f8beeb613f
--- /dev/null
+++ b/challenge-077/tyler-wardhaugh/clojure/resources/matrix1.txt
@@ -0,0 +1,3 @@
+O O X
+X O O
+X O O
diff --git a/challenge-077/tyler-wardhaugh/clojure/resources/matrix2.txt b/challenge-077/tyler-wardhaugh/clojure/resources/matrix2.txt
new file mode 100644
index 0000000000..d81104f1b2
--- /dev/null
+++ b/challenge-077/tyler-wardhaugh/clojure/resources/matrix2.txt
@@ -0,0 +1,4 @@
+O O X O
+X O O O
+X O O X
+O X O O
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
new file mode 120000
index 0000000000..5a32e17ef9
--- /dev/null
+++ b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch-2.clj
@@ -0,0 +1 @@
+ch_2.clj \ No newline at end of file
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
new file mode 100644
index 0000000000..6c6854eef1
--- /dev/null
+++ b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_2.clj
@@ -0,0 +1,50 @@
+(ns tw.weekly.ch-2
+ (:require [clojure.java.io :as io])
+ (:require [clojure.string :as str])
+ (:require [clojure.core.matrix :as mat])
+ (:require [pjstadig.reducible-stream :refer [decode-lines!]]))
+
+;;; Task description
+; TASK #2 › Lonely X
+; You are given m x n character matrix consists of O and X only.
+;
+; Write a script to count the total number of X surrounded by O only. Print 0 if none found.
+;;;
+
+(defn parse-matrix-file
+ "Parse matrix file and convert 'O's to 0s and 'X's to 1s. Return a multidimensional vector of ints."
+ [matrix-file]
+ (let [source (decode-lines! matrix-file)
+ xf (comp (map #(str/split % #"\s+")) (map #(replace {"O" 0, "X" 1} %)))]
+ (into [] xf source)))
+
+(defn extend-matrix
+ "Surround the matrix in a 'border' of 0s. That is, add rows before the first and after the last rows and add columns before the first and after the last columns."
+ [matrix]
+ (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)))
+
+(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]]]
+ (for [x (range 1 (inc m))
+ y (range 1 (inc n))
+ :let [submat (mat/submatrix extended [[(- x 1) 3] [(- y 1) 3]])]
+ :when (mat/e= canonical-lonely submat)]
+ [x y])))
+
+(defn -main
+ "Run Task 2 with a matrix file, defaulting to the 'matrix1.txt' file under the resources directory."
+ [& args]
+ (let [matrix-file (or (some-> args first io/file) (io/resource "matrix1.txt"))
+ matrix (parse-matrix-file matrix-file)]
+ (if-let [lonlies (find-lonelies matrix)]
+ (do
+ (printf "%d\n\nCoordinates:\n============\n" (count lonlies))
+ (run! println lonlies))
+ (println "No lonely Xs in matrix."))))
diff --git a/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj b/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj
index 7ab39d517a..e417556210 100644
--- a/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj
+++ b/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj
@@ -2,7 +2,7 @@
(:require [clojure.test :refer :all]
[clojure.java.io :as io]
[tw.weekly.ch-1 :refer [fibo-sum]]
- [tw.weekly.ch-2 :refer []]))
+ [tw.weekly.ch-2 :refer [parse-matrix-file find-lonelies]]))
(deftest ch-1
(testing "Task 1"
@@ -11,4 +11,8 @@
(deftest ch-2
(testing "Task 2"
- ))
+ (with-test
+ (defn f [filename]
+ (-> filename io/resource parse-matrix-file find-lonelies))
+ (is (= (f "matrix1.txt") [[1 3]]))
+ (is (= (f "matrix2.txt") [[1 3] [3 4]])))))