aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-11-23 11:09:29 -0800
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-11-23 11:59:44 -0800
commit4350804fe4f246c6e86ed016e378811fd308a294 (patch)
tree9dbd54ef04092f31a83bbe38ef94b7b1538b2def
parent54d4de3cf47f4350a04d8f5817e59513ad3b4660 (diff)
downloadperlweeklychallenge-club-4350804fe4f246c6e86ed016e378811fd308a294.tar.gz
perlweeklychallenge-club-4350804fe4f246c6e86ed016e378811fd308a294.tar.bz2
perlweeklychallenge-club-4350804fe4f246c6e86ed016e378811fd308a294.zip
Ch88 (Clojure): Task 2
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/resources/matrix-13
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/resources/matrix-24
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj12
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj35
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj11
-rw-r--r--challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj15
6 files changed, 79 insertions, 1 deletions
diff --git a/challenge-088/tyler-wardhaugh/clojure/resources/matrix-1 b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-1
new file mode 100644
index 0000000000..32009c9873
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-1
@@ -0,0 +1,3 @@
+[ 1, 2, 3 ]
+[ 4, 5, 6 ]
+[ 7, 8, 9 ]
diff --git a/challenge-088/tyler-wardhaugh/clojure/resources/matrix-2 b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-2
new file mode 100644
index 0000000000..ebee37d5d1
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/clojure/resources/matrix-2
@@ -0,0 +1,4 @@
+[ 1, 2, 3, 4 ]
+[ 5, 6, 7, 8 ]
+[ 9, 10, 11, 12 ]
+[ 13, 14, 15, 16 ]
diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj
new file mode 100644
index 0000000000..171b6fcb2e
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c88.core
+ (:require [tw.weekly.c88.t1 :as t1])
+ (:require [tw.weekly.c88.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1:")
+ (t1/-main)
+ (println "\nTask #2:")
+ (t2/-main))
diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj
new file mode 100644
index 0000000000..5ebb1933c0
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/t2.clj
@@ -0,0 +1,35 @@
+(ns tw.weekly.c88.t2
+ (:require [tw.weekly.c88.util :as util])
+ (:require [clojure.java.io :as io])
+ (:require [clojure.pprint :refer [cl-format]])
+ (:require [clojure.core.matrix :as m]))
+
+;;;
+; Task description for TASK #2 › Spiral Matrix
+;;;
+
+(defn make-matrix
+ "Instantiate a matrix from a matrix file"
+ [matrix-file]
+ (-> matrix-file util/parse-matrix-file m/matrix))
+
+(defn rotate-matrix
+ "Rotate a matrix counterclockwise"
+ [matrix]
+ (-> matrix m/transpose reverse))
+
+(defn spiral-matrix
+ "Return a sequence of matrix items in spiral order."
+ [matrix]
+ (when (m/eseq matrix)
+ (let [row (m/get-row matrix 0)
+ remaining-rows (-> matrix (m/select :rest :all) m/rows)]
+ (concat row (-> remaining-rows rotate-matrix spiral-matrix)))))
+
+(defn -main
+ "Run Task 2 with a given file containing a matrix, defaulting
+ to the example given in the task description."
+ [& args]
+ (let [matrix-file (or (some-> args first io/file) (io/resource "matrix-1"))
+ mseq (-> matrix-file make-matrix spiral-matrix)]
+ (cl-format true "[ ~{~a~^, ~} ]~%" mseq)))
diff --git a/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj
new file mode 100644
index 0000000000..453e2ff33c
--- /dev/null
+++ b/challenge-088/tyler-wardhaugh/clojure/src/tw/weekly/c88/util.clj
@@ -0,0 +1,11 @@
+(ns tw.weekly.c88.util
+ (:require [clojure.java.io :as io])
+ (:require [clojure.edn :as edn]))
+
+(defn parse-matrix-file
+ "Parse a matrix file and return a matrix"
+ [matrix-file]
+ (with-open [in (io/reader matrix-file)]
+ (-> (slurp in)
+ (as-> x (str \[ x \]))
+ edn/read-string)))
diff --git a/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj b/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj
index 0d3f2219ee..cd82616ee4 100644
--- a/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj
+++ b/challenge-088/tyler-wardhaugh/clojure/test/tw/weekly/c88_test.clj
@@ -1,8 +1,21 @@
(ns tw.weekly.c88-test
(:require [clojure.test :refer [deftest is testing]]
- [tw.weekly.c88.t1 :refer [array-of-product]]))
+ [clojure.java.io :as io]
+ [tw.weekly.c88.t1 :refer [array-of-product]]
+ [tw.weekly.c88.t2 :refer [make-matrix spiral-matrix]]))
(deftest task-1
(testing "Task 1 Array of Product"
(is (= [24 60 120 30 40] (array-of-product [5 2 1 4 3])))
(is (= [12 24 6 8] (array-of-product [2 1 4 3])))))
+
+(def task-2-helper
+ ^{:doc "Helper function to test Task 2"}
+ (comp spiral-matrix make-matrix io/resource))
+
+(deftest task-2
+ (testing "Task 2 Spiral Matrix"
+ (is (= [1 2 3 6 9 8 7 4 5]
+ (task-2-helper "matrix-1")))
+ (is (= [1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10]
+ (task-2-helper "matrix-2")))))