diff options
8 files changed, 116 insertions, 13 deletions
diff --git a/challenge-187/tyler-wardhaugh/clojure/README.md b/challenge-187/tyler-wardhaugh/clojure/README.md index 34ce87899e..e442ac496d 100644 --- a/challenge-187/tyler-wardhaugh/clojure/README.md +++ b/challenge-187/tyler-wardhaugh/clojure/README.md @@ -1,6 +1,6 @@ -# c186 +# c187 -The Weekly Challenge — #186 — Tyler Wardhaugh +The Weekly Challenge — #187 — Tyler Wardhaugh ## Usage @@ -8,12 +8,9 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started# Run Task #1: - $ clojure -M:t1 A B + $ clojure -M:t1 N # ... or ... - $ bb run task-1 A B - - # Alternatively, to run it via Babashka: - $ bb run task-1-bb A B + $ bb run task-1 N Run Task #2: @@ -21,6 +18,9 @@ Run Task #2: # ... or ... $ bb run task-2 N + # Alternatively, to run it via Babashka: + $ bb run task-2-bb N + Run the project's tests (which are samples from the task descriptions): $ clojure -T:build test diff --git a/challenge-187/tyler-wardhaugh/clojure/bb.edn b/challenge-187/tyler-wardhaugh/clojure/bb.edn index 615ed942a8..56c2ecdbad 100644 --- a/challenge-187/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-187/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src" "resources"] - :deps {c186/c186 {:local/root "."}} + :deps {c187/c187 {:local/root "."}} :tasks { @@ -57,12 +57,12 @@ :task (run-task-clj :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1 *command-line-args*)} + :task (bb-no-go :t1 *command-line-args*)} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task-clj :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (bb-no-go :t2 *command-line-args*)} + :task (run-task-bb :t2 *command-line-args*)} } } diff --git a/challenge-187/tyler-wardhaugh/clojure/build.clj b/challenge-187/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..4089e55843 --- /dev/null +++ b/challenge-187/tyler-wardhaugh/clojure/build.clj @@ -0,0 +1,19 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [org.corfield.build :as bb])) + +(def lib 'net.clojars.c187/c187) +(def version "0.1.0-SNAPSHOT") +(def main 'c187.c187) + +(defn test "Run the tests." [opts] + (bb/run-tests opts)) + +(def clean bb/clean) + +(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts] + (-> opts + (assoc :lib lib :version version :main main) + (bb/run-tests) + (bb/clean) + (bb/uber))) diff --git a/challenge-187/tyler-wardhaugh/clojure/deps.edn b/challenge-187/tyler-wardhaugh/clojure/deps.edn index 5f5705f98f..66dac0033f 100644 --- a/challenge-187/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-187/tyler-wardhaugh/clojure/deps.edn @@ -1,8 +1,10 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.11.1"}} + :deps {org.clojure/clojure {:mvn/version "1.11.1"} + henryw374/cljc.java-time {:git/url "https://github.com/henryw374/cljc.java-time" + :git/sha "85d1501e8833bd731d4bce8ea73eb801528f24ee"}} :aliases - {:t1 {:main-opts ["-m" "c186.t1"]} - :t2 {:main-opts ["-m" "c186.t2"]} + {:t1 {:main-opts ["-m" "c187.t1"]} + :t2 {:main-opts ["-m" "c187.t2"]} :build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.8.3" :git/sha "7ac1f8d" ;; since we're building an app uberjar, we do not diff --git a/challenge-187/tyler-wardhaugh/clojure/src/c187/t1.clj b/challenge-187/tyler-wardhaugh/clojure/src/c187/t1.clj new file mode 100644 index 0000000000..dc1312d210 --- /dev/null +++ b/challenge-187/tyler-wardhaugh/clojure/src/c187/t1.clj @@ -0,0 +1,36 @@ +(ns c187.t1 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]] + [clojure.set :as set] + [cljc.java-time.format.date-time-formatter :as dtf] + [cljc.java-time.local-date :as ld] + [cljc.java-time.month-day :as md])) + +(def ^{:doc "A non-leap year to cast the dates in."} YEAR 1971) +(def ^{:doc "The format we expect dates in."} DATE-FMT (dtf/of-pattern "dd-MM")) + +(def DEFAULT-INPUT + [[{:name "Foo" :SD "12-01" :ED "20-01"} + {:name "Bar" :SD "15-01" :ED "18-01"}]]) + +(defn ->date + [s] + (-> s (md/parse DATE-FMT) (md/at-year YEAR))) + +(defn map->date-range + [& {:keys [SD ED]}] + (let [start (->date SD) + end (->date ED)] + (->> (iterate #(ld/plus-days % 1) start) + (into #{} (take-while #(not (ld/is-after % end))))))) + +(defn overlap + [coll] + (->> coll (map map->date-range) (apply set/intersection) count)) + +(defn -main + "Run Task 1 with a given input N, defaulting to the first example from + the task description." + [& args] + (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (cl-format true "~a day~:p" (overlap N)))) diff --git a/challenge-187/tyler-wardhaugh/clojure/src/c187/t2.clj b/challenge-187/tyler-wardhaugh/clojure/src/c187/t2.clj new file mode 100644 index 0000000000..102e046192 --- /dev/null +++ b/challenge-187/tyler-wardhaugh/clojure/src/c187/t2.clj @@ -0,0 +1,18 @@ +(ns c187.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +(def DEFAULT-INPUT ['(1, 2, 3, 2)]) + +(defn magical-triplet + [coll] + (when-let [[a b c] (sort > coll)] + (when (and a b c (< a (+ b c))) + [a b c]))) + +(defn -main + "Run Task 2 with a given input N, defaulting to the first example from + the task description." + [& args] + (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (cl-format true "(~{~a~^, ~})~%" (magical-triplet N)))) diff --git a/challenge-187/tyler-wardhaugh/clojure/test/c187/t1_test.clj b/challenge-187/tyler-wardhaugh/clojure/test/c187/t1_test.clj new file mode 100644 index 0000000000..061f0cde60 --- /dev/null +++ b/challenge-187/tyler-wardhaugh/clojure/test/c187/t1_test.clj @@ -0,0 +1,14 @@ +(ns c187.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c187.t1 :refer [overlap]])) + +(deftest task-1 + (testing "Task 1 produces the correct results" + (is (= 4 (overlap [{:name "Foo" :SD "12-01" :ED "20-01"} + {:name "Bar" :SD "15-01" :ED "18-01"}]))) + (is (= 0 (overlap [{:name "Foo" :SD "02-03" :ED "12-03"} + {:name "Bar" :SD "13-03" :ED "14-03"}]))) + (is (= 2 (overlap [{:name "Foo" :SD "02-03" :ED "12-03"} + {:name "Bar" :SD "11-03" :ED "15-03"}]))) + (is (= 4 (overlap [{:name "Foo" :SD "30-03" :ED "05-04"} + {:name "Bar" :SD "28-03" :ED "02-04"}]))))) diff --git a/challenge-187/tyler-wardhaugh/clojure/test/c187/t2_test.clj b/challenge-187/tyler-wardhaugh/clojure/test/c187/t2_test.clj new file mode 100644 index 0000000000..a206d05df7 --- /dev/null +++ b/challenge-187/tyler-wardhaugh/clojure/test/c187/t2_test.clj @@ -0,0 +1,14 @@ +(ns c187.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c187.t2 :refer [magical-triplet]])) + +(deftest task-2 + (testing "Task 2 produces the correct results" + (is (= [3 2 2] (magical-triplet '(1, 2, 3, 2)))) + (is (nil? (magical-triplet '(1, 3, 2)))) + (is (nil? (magical-triplet '(1, 1, 2, 3)))) + (is (= [4 3 2] (magical-triplet '(2, 4, 3))))) + (testing "... with less than 3 elements" + (is (nil? (magical-triplet [3 2]))) + (is (nil? (magical-triplet [1]))) + (is (nil? (magical-triplet []))))) |
