From 410749b2ee37521a4bcdc5f287c4c995f5501bbb Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 14 Jun 2021 13:57:19 -0700 Subject: Ch117: prep for challenge --- challenge-117/tyler-wardhaugh/clojure/README.md | 12 +++--- challenge-117/tyler-wardhaugh/clojure/bb.edn | 43 ++++++++++++++-------- challenge-117/tyler-wardhaugh/clojure/deps.edn | 19 ++++++---- challenge-117/tyler-wardhaugh/clojure/pom.xml | 18 +++------ .../tyler-wardhaugh/clojure/resources/input.txt | 14 +++++++ .../clojure/src/tw/weekly/c117/core.clj | 12 ++++++ .../clojure/src/tw/weekly/c117/t1.clj | 17 +++++++++ .../clojure/src/tw/weekly/c117/t2.clj | 18 +++++++++ .../clojure/test/tw/weekly/c117_test.clj | 12 ++++++ 9 files changed, 123 insertions(+), 42 deletions(-) create mode 100644 challenge-117/tyler-wardhaugh/clojure/resources/input.txt create mode 100644 challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/core.clj create mode 100644 challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj create mode 100644 challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj create mode 100644 challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj 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..d6814eab4a 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,9 +30,35 @@ (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*)} @@ -59,20 +85,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..803cdffd33 100644 --- a/challenge-117/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-117/tyler-wardhaugh/clojure/deps.edn @@ -1,15 +1,18 @@ {: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"}} :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..e133db1508 100644 --- a/challenge-117/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-117/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ 4.0.0 tw.weekly - tw.weekly.c102 + tw.weekly.c117 0.1.0-SNAPSHOT - tw.weekly.c102 - Challenge #102 - https://github.com/tw.weekly/tw.weekly.c102 + tw.weekly.c117 + Challenge #117 + https://github.com/tw.weekly/tw.weekly.c117 Eclipse Public License @@ -15,20 +15,14 @@ - Tyler + Tyler Wardhaugh - - https://github.com/tw.weekly/tw.weekly.c102 - scm:git:git://github.com/tw.weekly/tw.weekly.c102.git - scm:git:ssh://git@github.com/tw.weekly/tw.weekly.c102.git - HEAD - org.clojure clojure - 1.10.1 + 1.10.3 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..661a1f8fc5 --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj @@ -0,0 +1,17 @@ +(ns tw.weekly.c117.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Missing Row +;;; +(def DEFAULT-INPUT []) + +(defn missing-row + [file]) + +(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 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..48a8519f75 --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj @@ -0,0 +1,18 @@ +(ns tw.weekly.c117.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Find Possible Paths +;;; +(def DEFAULT-INPUT [2]) + +(defn find-possible-paths + "" + [n]) + +(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 (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..0516bce664 --- /dev/null +++ b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c117-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c117.t1 :refer [missing-row]] + [tw.weekly.c117.t2 :refer [find-possible-paths]])) + +(deftest task-1 + (testing "Task 1, Missing Row" + )) + +(deftest task-2 + (testing "Task 2, Find Possible Paths" + )) -- cgit From 3d37b2d24933141599134e34af2a9fa3e9ee1c58 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Mon, 14 Jun 2021 14:54:56 -0700 Subject: Ch117 (Clojure): Task 1 --- challenge-117/tyler-wardhaugh/clojure/bb.edn | 4 ++- challenge-117/tyler-wardhaugh/clojure/deps.edn | 3 +- challenge-117/tyler-wardhaugh/clojure/pom.xml | 5 +++ .../clojure/resources/input-missing-first.txt | 14 ++++++++ .../clojure/resources/input-missing-last.txt | 14 ++++++++ .../clojure/src/tw/weekly/c117/t1.clj | 40 +++++++++++++++++++--- .../clojure/test/tw/weekly/c117_test.clj | 15 ++++++-- 7 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 challenge-117/tyler-wardhaugh/clojure/resources/input-missing-first.txt create mode 100644 challenge-117/tyler-wardhaugh/clojure/resources/input-missing-last.txt diff --git a/challenge-117/tyler-wardhaugh/clojure/bb.edn b/challenge-117/tyler-wardhaugh/clojure/bb.edn index d6814eab4a..0409645368 100644 --- a/challenge-117/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-117/tyler-wardhaugh/clojure/bb.edn @@ -63,7 +63,9 @@ :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*)} diff --git a/challenge-117/tyler-wardhaugh/clojure/deps.edn b/challenge-117/tyler-wardhaugh/clojure/deps.edn index 803cdffd33..b971f3081f 100644 --- a/challenge-117/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-117/tyler-wardhaugh/clojure/deps.edn @@ -1,5 +1,6 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"}} + :deps {org.clojure/clojure {:mvn/version "1.10.3"} + net.cgrand/xforms {:mvn/version "0.19.2"}} :aliases {:test {:extra-paths ["test"] diff --git a/challenge-117/tyler-wardhaugh/clojure/pom.xml b/challenge-117/tyler-wardhaugh/clojure/pom.xml index e133db1508..ed74b5497a 100644 --- a/challenge-117/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-117/tyler-wardhaugh/clojure/pom.xml @@ -24,6 +24,11 @@ clojure 1.10.3 + + net.cgrand + xforms + 0.19.2 + src 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/src/tw/weekly/c117/t1.clj b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj index 661a1f8fc5..c9e12ee4f6 100644 --- a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t1.clj @@ -1,17 +1,47 @@ (ns tw.weekly.c117.t1 - (:require [clojure.edn :as edn])) + (: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 []) +(def DEFAULT-INPUT [(io/resource "input.txt")]) +(def FIRST-ELEM 1) +(def LAST-ELEM 15) -(defn missing-row - [file]) +(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 edn/read-string)) DEFAULT-INPUT)] + (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/test/tw/weekly/c117_test.clj b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj index 0516bce664..d9547ea900 100644 --- a/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj +++ b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj @@ -1,11 +1,20 @@ (ns tw.weekly.c117-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c117.t1 :refer [missing-row]] - [tw.weekly.c117.t2 :refer [find-possible-paths]])) + [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" -- cgit From ab4f37311d4eb2d3f84159b74fb49244940041f1 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Wed, 16 Jun 2021 22:07:24 -0700 Subject: Ch117 (Clojure): Task 2 --- .../clojure/src/tw/weekly/c117/t2.clj | 24 ++++++++++++++++++---- .../clojure/test/tw/weekly/c117_test.clj | 10 +++++++-- 2 files changed, 28 insertions(+), 6 deletions(-) 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 index 48a8519f75..00df46cf0f 100644 --- a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj +++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj @@ -1,18 +1,34 @@ (ns tw.weekly.c117.t2 - (:require [clojure.edn :as edn])) + (: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 - "" - [n]) + "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)] - (println (find-possible-paths N)))) + (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 index d9547ea900..d59231653f 100644 --- a/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj +++ b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj @@ -2,7 +2,7 @@ (: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]])) + [tw.weekly.c117.t2 :refer [find-possible-paths]])) (deftest task-1 (testing "Task 1, Missing Row" @@ -18,4 +18,10 @@ (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))))) -- cgit