diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-30 09:34:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-30 09:34:46 +0100 |
| commit | 89589be5fc1a7b92d382c905acc57216a0513450 (patch) | |
| tree | b3d3084e99b98d122a14401d4b30fbeac884c957 | |
| parent | 979144e452a65703e7845a166d7c94ba6f89e37f (diff) | |
| parent | ed4136e5ec2a060eef0425ca51cf2878848e0980 (diff) | |
| download | perlweeklychallenge-club-89589be5fc1a7b92d382c905acc57216a0513450.tar.gz perlweeklychallenge-club-89589be5fc1a7b92d382c905acc57216a0513450.tar.bz2 perlweeklychallenge-club-89589be5fc1a7b92d382c905acc57216a0513450.zip | |
Merge pull request #8160 from tylerw/tw/challenge-218b
Ch218: implement Task 2 in Clojure
| -rw-r--r-- | challenge-218/tyler-wardhaugh/clojure/deps.edn | 3 | ||||
| -rw-r--r-- | challenge-218/tyler-wardhaugh/clojure/src/c218/t2.clj | 46 | ||||
| -rw-r--r-- | challenge-218/tyler-wardhaugh/clojure/test/c218/t2_test.clj | 9 |
3 files changed, 57 insertions, 1 deletions
diff --git a/challenge-218/tyler-wardhaugh/clojure/deps.edn b/challenge-218/tyler-wardhaugh/clojure/deps.edn index 3d77f22cca..27d77a66a8 100644 --- a/challenge-218/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-218/tyler-wardhaugh/clojure/deps.edn @@ -1,5 +1,6 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.11.1"}} + :deps {org.clojure/clojure {:mvn/version "1.11.1"} + net.mikera/core.matrix {:mvn/version "0.63.0"}} :aliases {:t1 {:main-opts ["-m" "c218.t1"]} :t2 {:main-opts ["-m" "c218.t2"]} diff --git a/challenge-218/tyler-wardhaugh/clojure/src/c218/t2.clj b/challenge-218/tyler-wardhaugh/clojure/src/c218/t2.clj new file mode 100644 index 0000000000..483bc5aa57 --- /dev/null +++ b/challenge-218/tyler-wardhaugh/clojure/src/c218/t2.clj @@ -0,0 +1,46 @@ +(ns c218.t2 + (:require + [clojure.edn :as edn] + [clojure.core.matrix :as mat] + [clojure.string :as str])) + +(def DEFAULT-INPUT + [[[0 0 1 1] + [1 0 1 0] + [1 1 0 0]]]) + +(defn score + [m] + (-> (map #(-> (map int %) str/join (Long/parseLong 2))) + (transduce + 0 (mat/rows m)))) + +(defn max-matrix-score + [coll] + (let [m (mat/mutable coll) + n-dim (mat/dimension-count m 0)] + + ; maximize rows + (loop [[r & rs] (mat/rows m) + i 0] + (when r + (when (zero? (mat/mget r 0)) + (mat/set-row! m i (mat/eif r 0 1))) + (recur rs (inc i)))) + + ; maximize columns + (loop [[c & cs] (mat/columns m) + i 0] + (when c + (when (and (pos? i) + (<= (mat/non-zero-count c) (quot n-dim 2))) + (mat/set-column! m i (mat/eif c 0 1))) + (recur cs (inc i)))) + + (score m))) + +(defn -main + "Run Task 2 with a given input COLL, defaulting to the first example from the + task description." + [& args] + (let [[coll] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (max-matrix-score coll)))) diff --git a/challenge-218/tyler-wardhaugh/clojure/test/c218/t2_test.clj b/challenge-218/tyler-wardhaugh/clojure/test/c218/t2_test.clj new file mode 100644 index 0000000000..371c8596f4 --- /dev/null +++ b/challenge-218/tyler-wardhaugh/clojure/test/c218/t2_test.clj @@ -0,0 +1,9 @@ +(ns c218.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c218.t2 :refer [max-matrix-score]])) + +(deftest task-2 + (testing "Task 2 produces the correct results from examples in the description" + (is (= 39 (max-matrix-score [[0 0 1 1] + [1 0 1 0] + [1 1 0 0]]))))) |
