diff options
7 files changed, 114 insertions, 24 deletions
diff --git a/challenge-102/tyler-wardhaugh/clojure/README.md b/challenge-102/tyler-wardhaugh/clojure/README.md index 372957fa28..fda553ce5e 100644 --- a/challenge-102/tyler-wardhaugh/clojure/README.md +++ b/challenge-102/tyler-wardhaugh/clojure/README.md @@ -1,13 +1,13 @@ -# tw.weekly.c101 +# tw.weekly.c102 -The Weekly Challenge - #101 - Tyler Wardhaugh +The Weekly Challenge - #102 - Tyler Wardhaugh ## Usage Run the project directly (shows default output from both tasks): - $ clojure -M -m tw.weekly.c101.core + $ clojure -M -m tw.weekly.c102.core Run the project's tests (which are samples from the task descriptions): @@ -15,11 +15,11 @@ Run the project's tests (which are samples from the task descriptions): Run Task #1 with input - $ clojure -M -m tw.weekly.c101.t1 A + $ clojure -M -m tw.weekly.c102.t1 N Run Task #2 with input: - $ clojure -M -m tw.weekly.c101.t2 A B C + $ clojure -M -m tw.weekly.c102.t2 N ## Project Template diff --git a/challenge-102/tyler-wardhaugh/clojure/deps.edn b/challenge-102/tyler-wardhaugh/clojure/deps.edn index 5a7bdda9e2..072d0028a0 100644 --- a/challenge-102/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-102/tyler-wardhaugh/clojure/deps.edn @@ -1,7 +1,6 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/math.numeric-tower {:mvn/version "0.0.4"} - net.mikera/core.matrix {:mvn/version "0.62.0"}} + clj-http/clj-http {:mvn/version "3.12.1"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} @@ -12,5 +11,5 @@ :main-opts ["-m" "cognitect.test-runner" "-d" "test"]} :uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.0.99"}} - :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c100.jar" - "-C" "-m" "tw.weekly.c100"]}}} + :main-opts ["-m" "hf.depstar.uberjar" "tw.weekly.c102.jar" + "-C" "-m" "tw.weekly.c102"]}}} diff --git a/challenge-102/tyler-wardhaugh/clojure/pom.xml b/challenge-102/tyler-wardhaugh/clojure/pom.xml index f3c877ab99..18df9a3fcf 100644 --- a/challenge-102/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-102/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.c101</artifactId> + <artifactId>tw.weekly.c102</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c101</name> - <description>Challenge #101</description> - <url>https://github.com/tw.weekly/tw.weekly.c101</url> + <name>tw.weekly.c102</name> + <description>Challenge #102</description> + <url>https://github.com/tw.weekly/tw.weekly.c102</url> <licenses> <license> <name>Eclipse Public License</name> @@ -19,9 +19,9 @@ </developer> </developers> <scm> - <url>https://github.com/tw.weekly/tw.weekly.c101</url> - <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c101.git</connection> - <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c101.git</developerConnection> + <url>https://github.com/tw.weekly/tw.weekly.c102</url> + <connection>scm:git:git://github.com/tw.weekly/tw.weekly.c102.git</connection> + <developerConnection>scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c102.git</developerConnection> <tag>HEAD</tag> </scm> <dependencies> @@ -31,14 +31,9 @@ <version>1.10.1</version> </dependency> <dependency> - <groupId>org.clojure</groupId> - <artifactId>math.numeric-tower</artifactId> - <version>0.0.4</version> - </dependency> - <dependency> - <groupId>net.mikera</groupId> - <artifactId>core.matrix</artifactId> - <version>0.62.0</version> + <groupId>clj-http</groupId> + <artifactId>clj-http</artifactId> + <version>3.12.1</version> </dependency> </dependencies> <build> diff --git a/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/core.clj b/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/core.clj new file mode 100644 index 0000000000..61ca7a502f --- /dev/null +++ b/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c102.core + (:require [tw.weekly.c102.t1 :as t1]) + (:require [tw.weekly.c102.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/t1.clj b/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/t1.clj new file mode 100644 index 0000000000..7b2ff27de6 --- /dev/null +++ b/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/t1.clj @@ -0,0 +1,35 @@ +(ns tw.weekly.c102.t1 + (:require [clojure.edn :as edn] + [clojure.string :as str] + [clojure.pprint :refer [cl-format]] + [clj-http.client :as client])) + +;;; +; Task description for TASK #1 › Rare Numbers +;;; + +(def DEFAULT-INPUT 1) + +(def rn-cache "A cache of Rare Numbers" (atom {})) + +(defn fetch-rn-cache + "Fetch Rare Numbers from OEIS and put them into a map for use as a cache." + [& _] + (let [source (-> (client/get "https://oeis.org/A035519/b035519.txt") + :body + str/split-lines) + xf (map (fn [x] (->> (str/split x #"\s+" 2) second)))] + (group-by count (sequence xf source)))) + +(defn rare-numbers + "Generate Rare Numbers for given size n" + [n] + (let [cache (if (empty? @rn-cache) (swap! rn-cache fetch-rn-cache) @rn-cache)] + (get cache n))) + +(defn -main + "Run Task 1 using a list A, defaulting to the example given in the task + description." + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (cl-format true "~a digit~:p: ~#:[<none>~;~:*~{~a~^ ~}~]~%" N (rare-numbers N)))) diff --git a/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/t2.clj b/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/t2.clj new file mode 100644 index 0000000000..9e786f8362 --- /dev/null +++ b/challenge-102/tyler-wardhaugh/clojure/src/tw/weekly/c102/t2.clj @@ -0,0 +1,30 @@ +(ns tw.weekly.c102.t2 + (:require [clojure.edn :as edn] + [clojure.string :as str])) + +;;; +; Task description for TASK #2 › Hash-counting String +;;; + +(def DEFAULT-INPUT 1) + +(defn hash-counting-str + "Return a hash-counting str of lengh `n`." + [n] + (let [source (take (inc n) (cycle [false true])) + f (fn [[index result] hash] + (if (<= index 0) + (reduced result) + (if hash + ((juxt #(- index (count %)) #(conj result %)) (-> index inc str)) + [(dec index) (conj result \#)])))] + (->> + (reduce f [n []] source) + reverse + str/join))) + +(defn -main + "Run Task 2 with N, defaulting to the example given in the task description." + [& args] + (let [N (or (some-> args first edn/read-string) DEFAULT-INPUT)] + (println (hash-counting-str N)))) diff --git a/challenge-102/tyler-wardhaugh/clojure/test/tw/weekly/c102_test.clj b/challenge-102/tyler-wardhaugh/clojure/test/tw/weekly/c102_test.clj new file mode 100644 index 0000000000..c2dd80caa8 --- /dev/null +++ b/challenge-102/tyler-wardhaugh/clojure/test/tw/weekly/c102_test.clj @@ -0,0 +1,19 @@ +(ns tw.weekly.c102-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c102.t1 :refer [rare-numbers]] + [tw.weekly.c102.t2 :refer [hash-counting-str]])) + +(deftest task-1 + (testing "Task 1, Rare Numbers" + (is (= ["65"] (rare-numbers 2))) + (is (= ["621770"] (rare-numbers 6))) + (is (= ["281089082"] (rare-numbers 9))) + (is (= #{"2022652202" "2042832002"} (set (rare-numbers 10)))))) + +(deftest task-2 + (testing "Task 2, Hash-counting String" + (is (= "#" (hash-counting-str 1))) + (is (= "2#" (hash-counting-str 2))) + (is (= "#3#" (hash-counting-str 3))) + (is (= "#3#5#7#10#" (hash-counting-str 10))) + (is (= "2#4#6#8#11#14#" (hash-counting-str 14))))) |
