diff options
7 files changed, 90 insertions, 16 deletions
diff --git a/challenge-127/tyler-wardhaugh/clojure/README.md b/challenge-127/tyler-wardhaugh/clojure/README.md index 99185b88b1..e040ae4a94 100644 --- a/challenge-127/tyler-wardhaugh/clojure/README.md +++ b/challenge-127/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c126 +# tw.weekly.c127 -The Weekly Challenge - #126 - Tyler Wardhaugh +The Weekly Challenge - #127 - 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.c126.core + $ clojure -M -m tw.weekly.c127.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.c126.t1 N + $ clojure -M -m tw.weekly.c127.t1 S1 S2 # ... or ... - $ bb run task-1 N + $ bb run task-1 S1 S2 Run Task #2 with input: - $ clojure -M -m tw.weekly.c126.t2 F + $ clojure -M -m tw.weekly.c127.t2 I # ... or ... - $ bb run task-2 F + $ bb run task-2 I View available tasks Babashka can run: @@ -45,5 +45,4 @@ See [seancorfield/clj-new: Generate new projects based on clj, Boot, or Leininge Copyright © 2021 Tyler Wardhaugh -Distributed under the Eclipse Public License either version 1.0 or (at -your option) any later version. +Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version. diff --git a/challenge-127/tyler-wardhaugh/clojure/bb.edn b/challenge-127/tyler-wardhaugh/clojure/bb.edn index 463fcb5d14..4cd11817b8 100644 --- a/challenge-127/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-127/tyler-wardhaugh/clojure/bb.edn @@ -23,12 +23,12 @@ (defn run-task [task args] (let [clj-options (format "-M -m %s " (get-task-ns task))] - (clojure (apply str clj-options args)))) + (apply clojure clj-options args))) (defn run-task-bb [task args] (let [bb-cmd (format "bb -m %s " (get-task-ns task))] - (shell (apply str bb-cmd args))))) + (apply shell bb-cmd args)))) clean {:doc "Clean out temporary files" :task (run! fs/delete-tree [".nrepl-port" ".cpcache" ".lsp"])} @@ -69,9 +69,7 @@ :task (run-task :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (binding [*out* *err*] - (println "error: can't run Task 2 via Babashka because it depends on some incompatible libraries.") - (System/exit 1))} + :task (run-task-bb :t2 *command-line-args*)} both {:doc "Run both tasks (via clojure)" :task (do diff --git a/challenge-127/tyler-wardhaugh/clojure/deps.edn b/challenge-127/tyler-wardhaugh/clojure/deps.edn index 54198fdfe8..5b1400b27e 100644 --- a/challenge-127/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-127/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"} - net.mikera/core.matrix {:mvn/version "0.62.0"}} + :deps {org.clojure/clojure {:mvn/version "1.10.3"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} diff --git a/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/core.clj b/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/core.clj new file mode 100644 index 0000000000..4a0bb34914 --- /dev/null +++ b/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c127.core + (:require [tw.weekly.c127.t1 :as t1]) + (:require [tw.weekly.c127.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/t1.clj b/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/t1.clj new file mode 100644 index 0000000000..219c103b75 --- /dev/null +++ b/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/t1.clj @@ -0,0 +1,19 @@ +(ns tw.weekly.c127.t1 + (:require [clojure.edn :as edn] + [clojure.set :as set])) + +;;; +; Task description for TASK #1 › Disjoint Sets +;;; +(def DEFAULT-INPUT ['(1, 2, 5, 3, 4) '(4, 6, 7, 8, 9)]) + +(defn disjoint? + [s1 s2] + (empty? (set/intersection (set s1) (set s2)))) + +(defn -main + "Run Task 1 with a given input S1 and S2, defaulting to the first example + from the task description." + [& args] + (let [[S1 S2] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (if (disjoint? S1 S2) 1 0)))) diff --git a/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/t2.clj b/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/t2.clj new file mode 100644 index 0000000000..20180099e1 --- /dev/null +++ b/challenge-127/tyler-wardhaugh/clojure/src/tw/weekly/c127/t2.clj @@ -0,0 +1,31 @@ +(ns tw.weekly.c127.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #2 › Conflict Intervals +;;; +(def DEFAULT-INPUT ['[ (1,4), (3,5), (6,8), (12, 13), (3,20) ]]) + +(defn overlaps? + "Does the interval [a b] conflict with the interval [n m]?" + [[n m] [a b]] + (or (< n b) (> a m))) + +(defn conflict-intervals + "Determine the conflict intervals in coll." + [coll] + (-> + (fn [overlaps i v] + (transduce (comp (take i) (filter (partial overlaps? v))) + (completing (fn [overlaps _] (reduced (conj overlaps v)))) + overlaps + coll)) + (reduce-kv [] coll))) + +(defn -main + "Run Task 2 with a given input, defaulting to the first example from the + task description." + [& args] + (let [[I] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (cl-format true "[ ~{(~{~a~^,~})~^, ~} ]~%" (conflict-intervals I)))) diff --git a/challenge-127/tyler-wardhaugh/clojure/test/tw/weekly/c127_test.clj b/challenge-127/tyler-wardhaugh/clojure/test/tw/weekly/c127_test.clj new file mode 100644 index 0000000000..e71dd8b60f --- /dev/null +++ b/challenge-127/tyler-wardhaugh/clojure/test/tw/weekly/c127_test.clj @@ -0,0 +1,16 @@ +(ns tw.weekly.c127-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c127.t1 :refer [disjoint?]] + [tw.weekly.c127.t2 :refer [conflict-intervals]])) + +(deftest task-1 + (testing "Disjoint Sets" + (is (false? (disjoint? '(1, 2, 5, 3, 4) '(4, 6, 7, 8, 9)))) + (is (true? (disjoint? '(1, 3, 5, 7, 9) '(0, 2, 4, 6, 8)))))) + +(deftest task-2 + (testing "Task 2, Conflict Intervals" + (is (= (conflict-intervals '[ (1,4), (3,5), (6,8), (12, 13), (3,20) ]) + '[ (3,5), (3,20) ])) + (is (= (conflict-intervals '[ (3,4), (5,7), (6,9), (10, 12), (13,15) ]) + '[ (6,9) ])))) |
