aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-09-05 20:58:11 +0100
committerGitHub <noreply@github.com>2021-09-05 20:58:11 +0100
commit7e721cd8deec8e7bec05b20309d4aec40651fbbf (patch)
treec3f1dea15655022d87cc21b1c5cd6882098b5386
parent3f16604c743bc8227c65c86ab1c1e7eeede3bd46 (diff)
parentd429daf56bcb501cedd57d5add2447f3ddc28299 (diff)
downloadperlweeklychallenge-club-7e721cd8deec8e7bec05b20309d4aec40651fbbf.tar.gz
perlweeklychallenge-club-7e721cd8deec8e7bec05b20309d4aec40651fbbf.tar.bz2
perlweeklychallenge-club-7e721cd8deec8e7bec05b20309d4aec40651fbbf.zip
Merge pull request #4842 from tylerw/tw/challenge-128
Challenge 128
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/README.md14
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/bb.edn4
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/deps.edn3
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/resources/matrix1.txt3
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/resources/matrix2.txt3
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/core.clj12
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/matrix_util.clj44
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t1.clj40
-rw-r--r--challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj18
9 files changed, 132 insertions, 9 deletions
diff --git a/challenge-128/tyler-wardhaugh/clojure/README.md b/challenge-128/tyler-wardhaugh/clojure/README.md
index e040ae4a94..36e0cd8bb2 100644
--- a/challenge-128/tyler-wardhaugh/clojure/README.md
+++ b/challenge-128/tyler-wardhaugh/clojure/README.md
@@ -1,7 +1,7 @@
-# tw.weekly.c127
+# tw.weekly.c128
-The Weekly Challenge - #127 - Tyler Wardhaugh
+The Weekly Challenge - #128 - Tyler Wardhaugh
## Usage
@@ -9,7 +9,7 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started#
Run the project directly (shows default output from both tasks):
- $ clojure -M -m tw.weekly.c127.core
+ $ clojure -M -m tw.weekly.c128.core
# ... or ...
$ bb run both
@@ -21,15 +21,15 @@ Run the project's tests (which are samples from the task descriptions):
Run Task #1 with input
- $ clojure -M -m tw.weekly.c127.t1 S1 S2
+ $ clojure -M -m tw.weekly.c128.t1 M
# ... or ...
- $ bb run task-1 S1 S2
+ $ bb run task-1 M
Run Task #2 with input:
- $ clojure -M -m tw.weekly.c127.t2 I
+ $ clojure -M -m tw.weekly.c128.t2 A D
# ... or ...
- $ bb run task-2 I
+ $ bb run task-2 A D
View available tasks Babashka can run:
diff --git a/challenge-128/tyler-wardhaugh/clojure/bb.edn b/challenge-128/tyler-wardhaugh/clojure/bb.edn
index 4cd11817b8..70feb7d8ea 100644
--- a/challenge-128/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-128/tyler-wardhaugh/clojure/bb.edn
@@ -63,7 +63,9 @@
:task (run-task :t1 *command-line-args*)}
task-1-bb {:doc "Run Task 1 (via Babashka)"
- :task (run-task-bb :t1 *command-line-args*)}
+ :task (binding [*out* *err*]
+ (println "error: can't run Task 1 via Babashka because it depends on some incompatible libraries.")
+ (System/exit 1))}
task-2 {:doc "Run Task 2 (via clojure)"
:task (run-task :t2 *command-line-args*)}
diff --git a/challenge-128/tyler-wardhaugh/clojure/deps.edn b/challenge-128/tyler-wardhaugh/clojure/deps.edn
index 5b1400b27e..54198fdfe8 100644
--- a/challenge-128/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-128/tyler-wardhaugh/clojure/deps.edn
@@ -1,5 +1,6 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.10.3"}}
+ :deps {org.clojure/clojure {:mvn/version "1.10.3"}
+ net.mikera/core.matrix {:mvn/version "0.62.0"}}
:aliases
{:test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "1.1.0"}
diff --git a/challenge-128/tyler-wardhaugh/clojure/resources/matrix1.txt b/challenge-128/tyler-wardhaugh/clojure/resources/matrix1.txt
new file mode 100644
index 0000000000..109dd5b3d6
--- /dev/null
+++ b/challenge-128/tyler-wardhaugh/clojure/resources/matrix1.txt
@@ -0,0 +1,3 @@
+[ 1 0 0 0 1 0 ]
+[ 1 1 0 0 0 1 ]
+[ 1 0 0 0 0 0 ]
diff --git a/challenge-128/tyler-wardhaugh/clojure/resources/matrix2.txt b/challenge-128/tyler-wardhaugh/clojure/resources/matrix2.txt
new file mode 100644
index 0000000000..3ee68404d8
--- /dev/null
+++ b/challenge-128/tyler-wardhaugh/clojure/resources/matrix2.txt
@@ -0,0 +1,3 @@
+[ 0 0 1 1 ]
+[ 0 0 0 1 ]
+[ 0 0 1 0 ]
diff --git a/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/core.clj b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/core.clj
new file mode 100644
index 0000000000..3b6fa41737
--- /dev/null
+++ b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c128.core
+ (:require [tw.weekly.c128.t1 :as t1])
+ (:require [tw.weekly.c128.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1:")
+ (t1/-main)
+ (println "\nTask #2:")
+ (t2/-main))
diff --git a/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/matrix_util.clj b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/matrix_util.clj
new file mode 100644
index 0000000000..4ef66ead6e
--- /dev/null
+++ b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/matrix_util.clj
@@ -0,0 +1,44 @@
+(ns tw.weekly.c128.matrix-util
+ (:require [clojure.java.io :as io]
+ [clojure.pprint :as pp]
+ [clojure.string :as str]
+ [clojure.core.matrix :as m]))
+
+(defn parse-matrix-file
+ "Parse a matrix file and return a matrix"
+ ([matrix-file]
+ (parse-matrix-file matrix-file (comp)))
+ ([matrix-file xf]
+ (with-open [in (io/reader matrix-file)]
+ (into [] xf (line-seq in)))))
+
+(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."
+ [mat]
+ (let [n (m/dimension-count mat 1)
+ blank-row (vec (repeat (+ n 2) 0))
+ step-1 (reduce (fn [mat v] (conj mat (vec (concat [0] v [0])))) [blank-row] mat)]
+ (conj step-1 blank-row)))
+
+(defn rotate-matrix
+ "Rotate a matrix counterclockwise"
+ [mat]
+ (-> mat m/transpose reverse))
+
+(defn print-matrix
+ ([mat]
+ (print-matrix mat true))
+ ([mat stream]
+ (pp/cl-format stream "~{[ ~{~a~^ ~} ]~%~}" mat)))
+
+(defn pprint-matrix
+ "Pretty print a matrix"
+ [mat]
+ (let [ks (range (m/dimension-count mat 1))
+ tbl (with-out-str
+ (pp/print-table ks (map (partial zipmap ks) mat)))]
+ (-> tbl
+ (str/split #"\n")
+ (->> (drop 3)
+ (run! println)))))
diff --git a/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t1.clj b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t1.clj
new file mode 100644
index 0000000000..b535021f51
--- /dev/null
+++ b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t1.clj
@@ -0,0 +1,40 @@
+(ns tw.weekly.c128.t1
+ (:require [clojure.edn :as edn]
+ [clojure.java.io :as io]
+ [clojure.string :as str]
+ [clojure.pprint :refer [cl-format]]
+ [clojure.core.matrix :as m]
+ [tw.weekly.c128.matrix-util :as mu]))
+
+;;;
+; Task description for TASK #1 › Maximum Sub-Matrix
+;;;
+(def DEFAULT-INPUT [(io/resource "matrix1.txt")])
+
+(defn parse
+ [f]
+ (->> (mu/parse-matrix-file f (map edn/read-string))
+ (m/array :ndarry)))
+
+(defn find-max-submatrix
+ [mat]
+ (let [source (m/index-seq mat)
+ [m n] (m/shape mat)
+ gen-subm (fn [[i j]]
+ (when (zero? (m/mget mat i j))
+ (for [x (range i (inc m))
+ y (range j (inc n))
+ :let [subm (m/submatrix mat i (- x i) j (- y j))]
+ :when (m/zero-matrix? subm)]
+ (m/shape subm))))
+ xf (comp (map gen-subm) (filter seq) cat)
+ f (completing (fn [w v] (max-key (partial apply *) w v)))]
+ (transduce xf f [0 0] source)))
+
+(defn -main
+ "Run Task 1 with a given input M, defaulting to the first example from the
+ task description."
+ [& args]
+ (let [[F] (or (some->> args (map (comp io/file edn/read-string))) DEFAULT-INPUT)
+ [m n] (-> F parse find-max-submatrix)]
+ (-> (m/zero-matrix :ndarry m n) (m/fill 0) mu/print-matrix)))
diff --git a/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj b/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj
new file mode 100644
index 0000000000..940922fab4
--- /dev/null
+++ b/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj
@@ -0,0 +1,18 @@
+(ns tw.weekly.c128-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [clojure.java.io :as io]
+ [tw.weekly.c128.t1 :as t1]
+ #_[tw.weekly.c128.t2 :as t2]))
+
+(defn solve-t1
+ [f]
+ (-> f io/resource t1/parse t1/find-max-submatrix))
+
+(deftest task-1
+ (testing "Task 1, Maximum Sub-Matrix"
+ (is (= [2 3] (solve-t1 "matrix1.txt")))
+ (is (= [3 2] (solve-t1 "matrix2.txt")))))
+
+(deftest task-2
+ (testing "Task 2, Minimum Platforms"
+ ))