diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-14 10:10:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-14 10:10:19 +0000 |
| commit | fbc6c65e260ab8ee5040f25d0ccaabf4c1aa4383 (patch) | |
| tree | 016628d2bd4c5d97225963b1ad50c90bff19386f | |
| parent | a5bbc536b36e9b53af5479418c2f6761e48a0713 (diff) | |
| parent | 9c75eb4f419bba343e1a0479dc81961246bc5929 (diff) | |
| download | perlweeklychallenge-club-fbc6c65e260ab8ee5040f25d0ccaabf4c1aa4383.tar.gz perlweeklychallenge-club-fbc6c65e260ab8ee5040f25d0ccaabf4c1aa4383.tar.bz2 perlweeklychallenge-club-fbc6c65e260ab8ee5040f25d0ccaabf4c1aa4383.zip | |
Merge pull request #5207 from tylerw/tw/challenge-138
Challenge 138
9 files changed, 136 insertions, 15 deletions
diff --git a/challenge-138/tyler-wardhaugh/clojure/README.md b/challenge-138/tyler-wardhaugh/clojure/README.md index 371f142e4e..7d1b854cdc 100644 --- a/challenge-138/tyler-wardhaugh/clojure/README.md +++ b/challenge-138/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c137 +# tw.weekly.c138 -The Weekly Challenge - #137 - Tyler Wardhaugh +The Weekly Challenge - #138 - 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.c137.core + $ clojure -M -m tw.weekly.c138.core # ... or ... $ bb run both @@ -21,13 +21,13 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c137.t1 + $ clojure -M -m tw.weekly.c138.t1 N # ... or ... - $ bb run task-1 + $ bb run task-1 N Run Task #2 with input: - $ clojure -M -m tw.weekly.c137.t2 N + $ clojure -M -m tw.weekly.c138.t2 N # ... or ... $ bb run task-2 N diff --git a/challenge-138/tyler-wardhaugh/clojure/bb.edn b/challenge-138/tyler-wardhaugh/clojure/bb.edn index f0892047d3..be8af5209b 100644 --- a/challenge-138/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-138/tyler-wardhaugh/clojure/bb.edn @@ -30,7 +30,17 @@ ([task args] (run-task-bb task args (get-task-ns task))) ([task args task-ns] (let [bb-cmd (format "bb -m %s " task-ns)] - (apply shell bb-cmd args))))) + (apply shell bb-cmd args)))) + + (defn bb-no-go + [task & _] + (binding [*out* *err*] + (let [task-num (-> task name last (Character/digit 10))] + (println + (str "error: can't run Task " task-num + " via Babashka because it depends" + " on some incompatible libraries.")))) + (System/exit 1))) clean {:doc "Clean out temporary files" :task (run! fs/delete-tree @@ -63,16 +73,16 @@ c**** {:doc "CHALLENGE TASKS"} task-1 {:doc "Run Task 1 (via clojure)" - :task (run-task :t1 *command-line-args* "tw.weekly.c137.t1")} + :task (run-task :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1 *command-line-args* "tw.weekly.c137.t1")} + :task (bb-no-go :t1 *command-line-args*)} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (run-task-bb :t2 *command-line-args*)} + :task (bb-no-go :t2 *command-line-args*)} both {:doc "Run both tasks (via clojure)" :task (do diff --git a/challenge-138/tyler-wardhaugh/clojure/deps.edn b/challenge-138/tyler-wardhaugh/clojure/deps.edn index 5b1400b27e..15aa02e4d4 100644 --- a/challenge-138/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-138/tyler-wardhaugh/clojure/deps.edn @@ -1,5 +1,8 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"}} + :deps {org.clojure/clojure {:mvn/version "1.10.3"} + clojure.java-time/clojure.java-time {:mvn/version "0.3.3"} + org.threeten/threeten-extra {:mvn/version "1.7.0"} + org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} diff --git a/challenge-138/tyler-wardhaugh/clojure/pom.xml b/challenge-138/tyler-wardhaugh/clojure/pom.xml index 03a691f300..8f51ecb4bb 100644 --- a/challenge-138/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-138/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>tw.weekly</groupId> - <artifactId>tw.weekly.c137</artifactId> + <artifactId>tw.weekly.c138</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c137</name> - <description>Challenge #137</description> - <url>https://github.com/tw.weekly/tw.weekly.c137</url> + <name>tw.weekly.c138</name> + <description>Challenge #138</description> + <url>https://github.com/tw.weekly/tw.weekly.c138</url> <licenses> <license> <name>Eclipse Public License</name> @@ -24,6 +24,16 @@ <artifactId>clojure</artifactId> <version>1.10.3</version> </dependency> + <dependency> + <groupId>clojure.java-time</groupId> + <artifactId>clojure.java-time</artifactId> + <version>0.3.3</version> + </dependency> + <dependency> + <groupId>org.threeten</groupId> + <artifactId>threeten-extra</artifactId> + <version>1.7.0</version> + </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> diff --git a/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/core.clj b/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/core.clj new file mode 100644 index 0000000000..403661d6f1 --- /dev/null +++ b/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c138.core + (:require [tw.weekly.c138.t1 :as t1]) + (:require [tw.weekly.c138.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/t1.clj b/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/t1.clj new file mode 100644 index 0000000000..1cec5141e3 --- /dev/null +++ b/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/t1.clj @@ -0,0 +1,23 @@ +(ns tw.weekly.c138.t1 + (:require [clojure.edn :as edn] + [java-time :as j])) + +;;; +; Task description for TASK #1 › Workdays +;;; +(def DEFAULT-INPUT [2021]) + +(defn count-workdays + [year] + (let [start (j/local-date year 1 1) + end (j/plus start (j/years 1)) + source (j/iterate j/adjust start :next-working-day) + xf (take-while #(j/before? % end))] + (count (sequence xf source)))) + +(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)] + (println (count-workdays N)))) diff --git a/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/t2.clj b/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/t2.clj new file mode 100644 index 0000000000..714b0beb42 --- /dev/null +++ b/challenge-138/tyler-wardhaugh/clojure/src/tw/weekly/c138/t2.clj @@ -0,0 +1,46 @@ +(ns tw.weekly.c138.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]] + [clojure.string :as str] + [clojure.math.numeric-tower :refer [sqrt expt]])) + +;;; +; Task description for TASK #2 › Split Number +;;; +(def DEFAULT-INPUT [81]) + +(defn ->int + [n] (Integer/parseInt n 10)) + +(defn n->break [len n] + (-> n + (bit-shift-left 1) + (->> (cl-format nil "~v,'0b" len)) + (str/escape {\1 \+ \0 \-}))) + +(defn get-splits + [n] + (let [s (str n) + len (count s) + breaks (map (partial n->break len) (range 1 (expt 2 (dec len)))) + join-nums (fn [coll] + (when (not= [\+] coll) + (->> coll (remove #{\-}) (str/join "") ->int))) + xf (map (comp (partial keep join-nums) + (partial partition-by #{\+}) + (partial interleave s)))] + (sequence xf breaks))) + +(defn sum-of-splits? + [n] + (let [source (get-splits n) + xf (comp (map (partial reduce +)) (keep #(when (= % (sqrt n)) true))) + f (completing (fn [_ v] (reduced v)))] + (transduce xf f false source))) + +(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)] + (println (if (sum-of-splits? N) 1 0)))) diff --git a/challenge-138/tyler-wardhaugh/clojure/test/tw/weekly/c138/t1_test.clj b/challenge-138/tyler-wardhaugh/clojure/test/tw/weekly/c138/t1_test.clj new file mode 100644 index 0000000000..9d32ebaad2 --- /dev/null +++ b/challenge-138/tyler-wardhaugh/clojure/test/tw/weekly/c138/t1_test.clj @@ -0,0 +1,8 @@ +(ns tw.weekly.c138.t1-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c138.t1 :refer [count-workdays]])) + +(deftest examples + (testing "Examples from description" + (is (= 261 (count-workdays 2021))) + (is (= 262 (count-workdays 2020))))) diff --git a/challenge-138/tyler-wardhaugh/clojure/test/tw/weekly/c138/t2_test.clj b/challenge-138/tyler-wardhaugh/clojure/test/tw/weekly/c138/t2_test.clj new file mode 100644 index 0000000000..b2a3ef3bb1 --- /dev/null +++ b/challenge-138/tyler-wardhaugh/clojure/test/tw/weekly/c138/t2_test.clj @@ -0,0 +1,9 @@ +(ns tw.weekly.c138.t2-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c138.t2 :refer [sum-of-splits?]])) + +(deftest examples + (testing "Examples from description" + (is (true? (sum-of-splits? 81))) + (is (true? (sum-of-splits? 9801))) + (is (false? (sum-of-splits? 36))))) |
