aboutsummaryrefslogtreecommitdiff
path: root/challenge-152
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-20 23:11:19 +0000
committerGitHub <noreply@github.com>2022-02-20 23:11:19 +0000
commitf0244dcfa028bec9b1f88f7d0fdb054dc5192344 (patch)
tree317e8c68e8d690dc514c43ada69e784fbf5528dc /challenge-152
parent6bb0641af3ce3837899eda39228af1800e0b3411 (diff)
parent891bec3c30670850babb0b8ab8a7d5c6a28bc26c (diff)
downloadperlweeklychallenge-club-f0244dcfa028bec9b1f88f7d0fdb054dc5192344.tar.gz
perlweeklychallenge-club-f0244dcfa028bec9b1f88f7d0fdb054dc5192344.tar.bz2
perlweeklychallenge-club-f0244dcfa028bec9b1f88f7d0fdb054dc5192344.zip
Merge pull request #5681 from tylerw/tw/challenge-152
Challenge 152
Diffstat (limited to 'challenge-152')
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/README.md14
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/deps.edn7
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/src/c152/t1.clj18
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/src/c152/t2.clj33
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/test/c152/t1_test.clj12
-rw-r--r--challenge-152/tyler-wardhaugh/clojure/test/c152/t2_test.clj8
7 files changed, 100 insertions, 11 deletions
diff --git a/challenge-152/tyler-wardhaugh/clojure/README.md b/challenge-152/tyler-wardhaugh/clojure/README.md
index 69836da026..c842049b9d 100644
--- a/challenge-152/tyler-wardhaugh/clojure/README.md
+++ b/challenge-152/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
# c148
-The Weekly Challenge — #150 — Tyler Wardhaugh
+The Weekly Challenge — #152 — Tyler Wardhaugh
## Usage
@@ -8,21 +8,21 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started#
Run Task #1:
- $ clojure -M:t1 A B
+ $ clojure -M:t1 T
# ... or ...
- $ bb run task-1 A B
+ $ bb run task-1 T
# Alternatively, to run it via Babashka:
- $ bb run task-1-bb A B
+ $ bb run task-1-bb T
Run Task #2:
- $ clojure -M:t2
+ $ clojure -M:t2 R1 R2
# ... or ...
- $ bb run task-2
+ $ bb run task-2 R1 R2
# Alternatively, to run it via Babashka:
- $ bb run task-2-bb
+ $ bb run task-2-bb R1 R2
Run the project's tests (which are samples from the task descriptions):
diff --git a/challenge-152/tyler-wardhaugh/clojure/build.clj b/challenge-152/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..038aec1a3d
--- /dev/null
+++ b/challenge-152/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.c152/c152)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c152.c152)
+
+(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-152/tyler-wardhaugh/clojure/deps.edn b/challenge-152/tyler-wardhaugh/clojure/deps.edn
index f1e8a6e0ee..2af9fe3eba 100644
--- a/challenge-152/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-152/tyler-wardhaugh/clojure/deps.edn
@@ -1,9 +1,8 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.11.0-beta1"}
- com.hypirion/primes {:mvn/version "0.2.2"}}
+ :deps {org.clojure/clojure {:mvn/version "1.11.0-rc1"}}
:aliases
- {:t1 {:main-opts ["-m" "c150.t1"]}
- :t2 {:main-opts ["-m" "c150.t2"]}
+ {:t1 {:main-opts ["-m" "c152.t1"]}
+ :t2 {:main-opts ["-m" "c152.t2"]}
:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.6.3" :git/sha "9b8e09b"
;; since we're building an app uberjar, we do not
diff --git a/challenge-152/tyler-wardhaugh/clojure/src/c152/t1.clj b/challenge-152/tyler-wardhaugh/clojure/src/c152/t1.clj
new file mode 100644
index 0000000000..70789f7cb4
--- /dev/null
+++ b/challenge-152/tyler-wardhaugh/clojure/src/c152/t1.clj
@@ -0,0 +1,18 @@
+(ns c152.t1
+ (:require [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
+
+(def DEFAULT-INPUT [[[1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8]]])
+
+(defn min-triangle-sum-path
+ [triangle]
+ (map (partial reduce min) triangle))
+
+(defn -main
+ "Run Task 1 using a triangle T, defaulting to the example given in the
+ task description."
+ [& args]
+ (let [[T] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)
+ path (min-triangle-sum-path T)]
+ (cl-format true "Minimum Sum Path = ~{~a~^ + ~} => ~a~%"
+ path (reduce + path))))
diff --git a/challenge-152/tyler-wardhaugh/clojure/src/c152/t2.clj b/challenge-152/tyler-wardhaugh/clojure/src/c152/t2.clj
new file mode 100644
index 0000000000..cf4dbf27e3
--- /dev/null
+++ b/challenge-152/tyler-wardhaugh/clojure/src/c152/t2.clj
@@ -0,0 +1,33 @@
+(ns c152.t2
+ (:require [clojure.edn :as edn]))
+
+(def DEFAULT-INPUT ['[(-1,0), (2,2)] '[(0,-1), (4,4)]])
+(def ZERO-RECT [[0 0] [0 0]])
+
+(defn rectangle-area
+ [r]
+ (let [[[x1 y1] [x2 y2]] r]
+ (* (abs (- x1 x2))
+ (abs (- y1 y2)))))
+
+(defn overlap
+ [rs]
+ (let [firsts (map first rs)
+ seconds (map second rs)
+ bottom-left (map (partial apply max) (apply map vector firsts))
+ top-right (map (partial apply min) (apply map vector seconds))
+ diff (apply min (map - top-right bottom-left))]
+ (if (pos? diff) [bottom-left top-right] ZERO-RECT)))
+
+(defn total-area
+ [r1 r2]
+ (let [both (transduce (map rectangle-area) + 0 [r1 r2])
+ intersection (-> [r1 r2] overlap rectangle-area)]
+ (- both intersection)))
+
+(defn -main
+ "Run Task 2 using rectangles R1 & R2, defaulting to the example given in the
+ task description."
+ [& args]
+ (let [[R1 R2] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
+ (println (total-area R1 R2))))
diff --git a/challenge-152/tyler-wardhaugh/clojure/test/c152/t1_test.clj b/challenge-152/tyler-wardhaugh/clojure/test/c152/t1_test.clj
new file mode 100644
index 0000000000..bfa0e7ae26
--- /dev/null
+++ b/challenge-152/tyler-wardhaugh/clojure/test/c152/t1_test.clj
@@ -0,0 +1,12 @@
+(ns c152.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c152.t1 :refer [min-triangle-sum-path]]))
+
+(deftest examples
+ (testing "Examples from the test description"
+ (is (= [1 3 2 0 2]
+ (min-triangle-sum-path
+ [ [1], [5,3], [2,3,4], [7,1,0,2], [6,4,5,2,8] ])))
+ (is (= [5 2 1 0 1]
+ (min-triangle-sum-path
+ [ [5], [2,3], [4,1,5], [0,1,2,3], [7,2,4,1,9] ])))))
diff --git a/challenge-152/tyler-wardhaugh/clojure/test/c152/t2_test.clj b/challenge-152/tyler-wardhaugh/clojure/test/c152/t2_test.clj
new file mode 100644
index 0000000000..74f0ec3a11
--- /dev/null
+++ b/challenge-152/tyler-wardhaugh/clojure/test/c152/t2_test.clj
@@ -0,0 +1,8 @@
+(ns c152.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c152.t2 :refer [total-area]]))
+
+(deftest examples
+ (testing "Examples from the test description"
+ (is (= 22 (total-area '[(-1,0), (2,2)] '[(0,-1), (4,4)])))
+ (is (= 25 (total-area '[(-3,-1), (1,3)] '[(-1,-3), (2,2)])))))