aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2022-09-23 13:56:15 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2022-09-23 15:57:50 -0700
commit3470e5642d28b9bea7ff54aedc4a27f3d9fd9d96 (patch)
tree83b8eacdae521a4dec131e56e13ef556a16b7c39
parent7cd2927e6bb515d58ef38559f660e1f3ec3096d8 (diff)
downloadperlweeklychallenge-club-3470e5642d28b9bea7ff54aedc4a27f3d9fd9d96.tar.gz
perlweeklychallenge-club-3470e5642d28b9bea7ff54aedc4a27f3d9fd9d96.tar.bz2
perlweeklychallenge-club-3470e5642d28b9bea7ff54aedc4a27f3d9fd9d96.zip
Ch183: implement Task 1 in Clojure
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/README.md10
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/deps.edn8
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/src/c183/t1.clj15
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/src/c183/t2.clj27
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/test/c183/t1_test.clj10
-rw-r--r--challenge-183/tyler-wardhaugh/clojure/test/c183/t2_test.clj14
8 files changed, 96 insertions, 9 deletions
diff --git a/challenge-183/tyler-wardhaugh/clojure/README.md b/challenge-183/tyler-wardhaugh/clojure/README.md
index 5e0738c3b0..f96186bfeb 100644
--- a/challenge-183/tyler-wardhaugh/clojure/README.md
+++ b/challenge-183/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
-# c182
+# c183
-The Weekly Challenge — #182 — Tyler Wardhaugh
+The Weekly Challenge — #183 — Tyler Wardhaugh
## Usage
@@ -17,12 +17,12 @@ Run Task #1:
Run Task #2:
- $ clojure -M:t2 P1 [P2 ...]
+ $ clojure -M:t2 D1 D2
# ... or ...
- $ bb run task-2 P1 [P2 ...]
+ $ bb run task-2 D1 D2
# Alternatively, to run it via Babashka:
- $ bb run task-2-bb P1 [P2 ...]
+ $ bb run task-2-bb D1 D2
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-183/tyler-wardhaugh/clojure/bb.edn b/challenge-183/tyler-wardhaugh/clojure/bb.edn
index d1cdf9362e..576e6bc671 100644
--- a/challenge-183/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-183/tyler-wardhaugh/clojure/bb.edn
@@ -1,6 +1,6 @@
{
:paths ["src" "resources"]
- :deps {local/deps {:local/root "."}}
+ :deps {c183/c183 {:local/root "."}}
:tasks
{
diff --git a/challenge-183/tyler-wardhaugh/clojure/build.clj b/challenge-183/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..70966be819
--- /dev/null
+++ b/challenge-183/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.c183/c183)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c183.c183)
+
+(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-183/tyler-wardhaugh/clojure/deps.edn b/challenge-183/tyler-wardhaugh/clojure/deps.edn
index c3b389759b..fa24e59ef7 100644
--- a/challenge-183/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-183/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" "c182.t1"]}
- :t2 {:main-opts ["-m" "c182.t2"]}
+ {:t1 {:main-opts ["-m" "c183.t1"]}
+ :t2 {:main-opts ["-m" "c183.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-183/tyler-wardhaugh/clojure/src/c183/t1.clj b/challenge-183/tyler-wardhaugh/clojure/src/c183/t1.clj
new file mode 100644
index 0000000000..5fbb87eb4a
--- /dev/null
+++ b/challenge-183/tyler-wardhaugh/clojure/src/c183/t1.clj
@@ -0,0 +1,15 @@
+(ns c183.t1
+ (:require [clojure.edn :as edn]))
+
+(def DEFAULT-INPUT ['([1,2], [3,4], [5,6], [1,2])])
+
+; In Clojure, data is immutable (except for some specific circumstances), so a
+; set removes duplicates even when the elements are themselves lists.
+(def unique-array set)
+
+(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)]
+ (print (-> N unique-array seq))))
diff --git a/challenge-183/tyler-wardhaugh/clojure/src/c183/t2.clj b/challenge-183/tyler-wardhaugh/clojure/src/c183/t2.clj
new file mode 100644
index 0000000000..3df694b1b2
--- /dev/null
+++ b/challenge-183/tyler-wardhaugh/clojure/src/c183/t2.clj
@@ -0,0 +1,27 @@
+(ns c183.t2
+ (:require [clojure.pprint :refer [cl-format]]
+ [cljc.java-time.local-date :as ld]
+ [cljc.java-time.temporal.chrono-unit :as cu]))
+
+(def DEFAULT-INPUT ["2019-02-10" "2022-11-01"])
+
+(defn date-diff
+ [start end]
+ (let [s (ld/parse start)
+ e (ld/parse end)
+ years (cu/between cu/years s e)
+ days (cu/between cu/days (ld/plus-years s years) e)]
+ [years days]))
+
+(defn formatted-date-diff
+ [start end]
+ (let [[years days] (date-diff start end)
+ fmt "~[~*~:;~:*~a year~:p~[~:; ~]~]~:*~[~:;~:*~a day~:p~]"]
+ (cl-format nil fmt years days)))
+
+(defn -main
+ "Run Task 2 with a given input D1 and D2, defaulting to the first example
+ from the task description."
+ [& args]
+ (let [[D1 D2] (or args DEFAULT-INPUT)]
+ (print (formatted-date-diff D1 D2))))
diff --git a/challenge-183/tyler-wardhaugh/clojure/test/c183/t1_test.clj b/challenge-183/tyler-wardhaugh/clojure/test/c183/t1_test.clj
new file mode 100644
index 0000000000..4d5a428d64
--- /dev/null
+++ b/challenge-183/tyler-wardhaugh/clojure/test/c183/t1_test.clj
@@ -0,0 +1,10 @@
+(ns c183.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c183.t1 :refer [unique-array]]))
+
+(deftest task-1
+ (testing "Task 1 produces the correct result"
+ (is (= (set '([1,2], [3,4], [5,6]))
+ (unique-array '([1,2], [3,4], [5,6], [1,2]))))
+ (is (= (set '([9, 1], [3,7], [2,5]))
+ (unique-array '([9,1], [3,7], [2,5], [2,5]))))))
diff --git a/challenge-183/tyler-wardhaugh/clojure/test/c183/t2_test.clj b/challenge-183/tyler-wardhaugh/clojure/test/c183/t2_test.clj
new file mode 100644
index 0000000000..7e4a7fb71b
--- /dev/null
+++ b/challenge-183/tyler-wardhaugh/clojure/test/c183/t2_test.clj
@@ -0,0 +1,14 @@
+(ns c183.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c183.t2 :refer [formatted-date-diff]]))
+
+(deftest task-2
+ (testing "Task 2 produces the correct result"
+ (is (= (formatted-date-diff "2019-02-10" "2022-11-01") "3 years 264 days"))
+ (is (= (formatted-date-diff "2020-09-15" "2022-03-29") "1 year 195 days"))
+ (is (= (formatted-date-diff "2019-12-31" "2020-01-01") "1 day"))
+ (is (= (formatted-date-diff "2019-12-01" "2019-12-31") "30 days"))
+ (is (= (formatted-date-diff "2019-12-31" "2020-12-31") "1 year"))
+ (is (= (formatted-date-diff "2019-12-31" "2021-12-31") "2 years"))
+ (is (= (formatted-date-diff "2020-09-15" "2021-09-16") "1 year 1 day"))
+ (is (= (formatted-date-diff "2019-09-15" "2021-09-16") "2 years 1 day"))))