diff options
11 files changed, 221 insertions, 43 deletions
diff --git a/challenge-117/tyler-wardhaugh/clojure/README.md b/challenge-117/tyler-wardhaugh/clojure/README.md index 25387710fe..46dde45ce4 100644 --- a/challenge-117/tyler-wardhaugh/clojure/README.md +++ b/challenge-117/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c116 +# tw.weekly.c117 -The Weekly Challenge - #116 - Tyler Wardhaugh +The Weekly Challenge - #117 - 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.c116.core + $ clojure -M -m tw.weekly.c117.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.c116.t1 N + $ clojure -M -m tw.weekly.c117.t1 FILE # ... or ... - $ bb run task-1 N + $ bb run task-1 FILE Run Task #2 with input: - $ clojure -M -m tw.weekly.c116.t2 N + $ clojure -M -m tw.weekly.c117.t2 N # ... or ... $ bb run task-2 N diff --git a/challenge-117/tyler-wardhaugh/clojure/bb.edn b/challenge-117/tyler-wardhaugh/clojure/bb.edn index 6b36f27eb2..0409645368 100644 --- a/challenge-117/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-117/tyler-wardhaugh/clojure/bb.edn @@ -1,6 +1,6 @@ { :paths ["src"] - :deps {org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} + :deps {} :tasks { @@ -30,14 +30,42 @@ (let [bb-cmd (format "bb -m %s " (get-task-ns task))] (shell (apply str bb-cmd args))))) + clean {:doc "Clean out temporary files" + :task (run! fs/delete-tree [".nrepl-port" ".cpcache" ".lsp"])} + + generate-pom {:doc "Generate POM file" + :task (clojure "-X:deps mvn-pom")} + + generate-jar {:doc "Generate JAR file" + :depends [generate-pom] + :task (clojure "-X:jar")} + + publish {:doc "Publish branch via git-push to REPO (default: origin)" + :requires ([clojure.string :as str] + [babashka.process :as p :refer [process]]) + :task (let [repo (or (first *command-line-args*) "origin") + current-branch (-> (p/$ git branch --show-current) + p/check + :out + slurp + str/trim-newline) + cmd '[git push --force-with-lease --set-upstream] + args [repo current-branch]] + (-> (process (concat cmd args) {:inherit true}) + p/check))} + test {:doc "Run tests" :task (clojure "-M:test:runner")} + c**** {:doc "CHALLENGE TASKS"} + task-1 {:doc "Run Task 1 (via clojure)" :task (run-task :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1 *command-line-args*)} + :task (binding [*out* *err*] + (println "error: can't run Task 1 via Babashka because it depends on an incompatible library.") + (System/exit 1))} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task :t2 *command-line-args*)} @@ -59,20 +87,5 @@ (println "\nTask 2:") (run 'task-2-bb))} - - publish {:doc "Publish branch via git-push to REPO (default: origin)" - :requires ([clojure.string :as str] - [babashka.process :as p :refer [process]]) - :task (let [repo (or (first *command-line-args*) "origin") - current-branch (-> (p/$ git branch --show-current) - p/check - :out - slurp - str/trim-newline) - cmd '[git push --force-with-lease --set-upstream] - args [repo current-branch]] - (-> (process (concat cmd args) {:inherit true}) - p/check))} - } } diff --git a/challenge-117/tyler-wardhaugh/clojure/deps.edn b/challenge-117/tyler-wardhaugh/clojure/deps.edn index 71837236a5..b971f3081f 100644 --- a/challenge-117/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-117/tyler-wardhaugh/clojure/deps.edn @@ -1,15 +1,19 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} + :deps {org.clojure/clojure {:mvn/version "1.10.3"} + net.cgrand/xforms {:mvn/version "0.19.2"}} :aliases - {:test {:extra-paths ["test"] - :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} + {:test + {:extra-paths ["test"] + :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} + :runner {:extra-deps {com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner" - :sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}} + :sha "705ad25bbf0228b1c38d0244a36001c2987d7337"}} :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.c112.jar" - "-C" "-m" "tw.weekly.c112"]}}} + + :jar + {:replace-deps {com.github.seancorfield/depstar {:mvn/version "2.0.216"}} + :exec-fn hf.depstar/jar + :exec-args {:jar "tw-weekly.jar" :sync-pom true}}}} diff --git a/challenge-117/tyler-wardhaugh/clojure/pom.xml b/challenge-117/tyler-wardhaugh/clojure/pom.xml index b6737dd838..ed74b5497a 100644 --- a/challenge-117/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-117/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.c102</artifactId> + <artifactId>tw.weekly.c117</artifactId> <version>0.1.0-SNAPSHOT</version> - <name>tw.weekly.c102</name> - <description>Challenge #102</description> - <url>https://github.com/tw.weekly/tw.weekly.c102</url> + <name>tw.weekly.c117</name> + <description>Challenge #117</description> + <url>https://github.com/tw.weekly/tw.weekly.c117</url> <licenses> <license> <name>Eclipse Public License</name> @@ -15,20 +15,19 @@ </licenses> <developers> <developer> - <name>Tyler</name> + <name>Tyler Wardhaugh</name> </developer> </developers> - <scm> - <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> <dependency> <groupId>org.clojure</groupId> <artifactId>clojure</artifactId> - <version>1.10.1</version> + <version>1.10.3</version> + </dependency> + <dependency> + <groupId>net.cgrand</groupId> + <artifactId>xforms</artifactId> + <version>0.19.2</version> </dependency> </dependencies> <build> diff --git a/challenge-117/tyler-wardhaugh/clojure/resources/input-missing-first.txt b/challenge-117/tyler-wardhaugh/clojure/resources/input-missing-first.txt new file mode 100644 index 0000000000..1aa22cf6e4 --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/resources/input-missing-first.txt @@ -0,0 +1,14 @@ +11, Line Eleven +14, Line Fourteen +2, Line two +10, Line Ten +5, Line Five +4, Line Four +12, Line Twelve +6, Line Six +13, Line Thirteen +9, Line Nine +7, Line Seven +3, Line three +8, Line Eight +15, Line Fifteen diff --git a/challenge-117/tyler-wardhaugh/clojure/resources/input-missing-last.txt b/challenge-117/tyler-wardhaugh/clojure/resources/input-missing-last.txt new file mode 100644 index 0000000000..f3e1b20507 --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/resources/input-missing-last.txt @@ -0,0 +1,14 @@ +8, Line Eight +2, Line two +1, Line one +13, Line Thirteen +6, Line Six +4, Line Four +10, Line Ten +14, Line Fourteen +12, Line Twelve +9, Line Nine +5, Line Five +11, Line Eleven +7, Line Seven +3, Line three diff --git a/challenge-117/tyler-wardhaugh/clojure/resources/input.txt b/challenge-117/tyler-wardhaugh/clojure/resources/input.txt new file mode 100644 index 0000000000..5b9d9ab1ce --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/resources/input.txt @@ -0,0 +1,14 @@ +11, Line Eleven +1, Line one +9, Line Nine +13, Line Thirteen +2, Line two +6, Line Six +8, Line Eight +10, Line Ten +7, Line Seven +4, Line Four +14, Line Fourteen +3, Line three +15, Line Fifteen +5, Line Five diff --git a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/core.clj b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/core.clj new file mode 100644 index 0000000000..3bbe2db07c --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c117.core + (:require [tw.weekly.c117.t1 :as t1]) + (:require [tw.weekly.c117.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj new file mode 100644 index 0000000000..c9e12ee4f6 --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj @@ -0,0 +1,47 @@ +(ns tw.weekly.c117.t1 + (:require [clojure.edn :as edn] + [clojure.java.io :as io] + [clojure.set :as set] + [net.cgrand.xforms.io :as xio])) + +;;; +; Task description for TASK #1 › Missing Row +;;; +(def DEFAULT-INPUT [(io/resource "input.txt")]) +(def FIRST-ELEM 1) +(def LAST-ELEM 15) + +(defn get-nums + [file] + (->> (xio/lines-in file) + (into (sorted-set) (map edn/read-string)))) + +(defn missing-row-by-set-difference + [file] + (let [present (get-nums file) + population (set (range FIRST-ELEM (inc LAST-ELEM))) + missing (set/difference population present)] + (first missing))) + +(defn missing-row-by-incrementing + [file] + (let [present (conj (get-nums file) + (dec FIRST-ELEM) + (inc LAST-ELEM))] + (-> + (fn [prev curr] + (if (zero? (- curr prev 1)) + curr + (reduced (dec curr)))) + (reduce present)))) + +(def missing-row missing-row-by-set-difference) + +(defn -main + "Run Task 1 with a given input FILE, defaulting to the first example from the + task description." + [& args] + (let [[FILE] (or + (some->> args (map (comp io/file edn/read-string))) + DEFAULT-INPUT)] + (println (missing-row FILE)))) diff --git a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj new file mode 100644 index 0000000000..00df46cf0f --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj @@ -0,0 +1,34 @@ +(ns tw.weekly.c117.t2 + (:require [clojure.edn :as edn] + [clojure.pprint :refer [cl-format]])) + +;;; +; Task description for TASK #2 › Find Possible Paths +;;; +(def DEFAULT-INPUT [2]) + +(defn- paths + "Build up the results for all paths." + [r l h n curr results] + (if (= (+ r l) (+ r h) n) + (conj results curr) + (cond-> results + (< (+ r l) n) (concat (paths (inc r) l h n (conj curr :R) results) + (paths r (inc l) h n (conj curr :L) results)) + (< h l) (concat (paths r l (inc h) n (conj curr :H) results))))) + +(defn find-possible-paths + "Find all possible paths from top to the bottom right corner." + [n] + (let [results (paths 0 0 0 n [] [])] + (into + (sorted-set) + (map (fn [coll] (transduce (map name) str "" coll))) + results))) + +(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~^ ~}~%" (find-possible-paths N)))) diff --git a/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj new file mode 100644 index 0000000000..d59231653f --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj @@ -0,0 +1,27 @@ +(ns tw.weekly.c117-test + (:require [clojure.test :refer [deftest is testing]] + [clojure.java.io :as io] + [tw.weekly.c117.t1 :as t1] + [tw.weekly.c117.t2 :refer [find-possible-paths]])) + +(deftest task-1 + (testing "Task 1, Missing Row" + (let [example (first t1/DEFAULT-INPUT) + m-first (io/resource "input-missing-first.txt") + m-last (io/resource "input-missing-last.txt")] + (is (= 12 (t1/missing-row-by-incrementing example))) + (is (= 12 (t1/missing-row-by-set-difference example))) + (is (= 1 (t1/missing-row-by-incrementing m-first))) + (is (= 1 (t1/missing-row-by-set-difference m-first))) + (is (= 15 (t1/missing-row-by-incrementing m-last))) + (is (= 15 (t1/missing-row-by-set-difference m-last)))))) + +(deftest task-2 + (testing "Task 2, Find Possible Paths" + (is (= #{"LH" "R"} (find-possible-paths 1))) + (is (= #{"LHLH" "LHR" "LLHH" "LRH" "RLH" "RR"} (find-possible-paths 2))) + (is (= + #{"LHLHLH" "LHLHR" "LHLLHH" "LHLRH" "LHRLH" "LHRR" "LLHHLH" "LLHHR" + "LLHLHH" "LLHRH" "LLLHHH" "LLRHH" "LRHLH" "LRHR" "LRLHH" "LRRH" + "RLHLH" "RLHR" "RLLHH" "RLRH" "RRLH" "RRR"} + (find-possible-paths 3))))) |
