From 4d91ca6d56ca464c41c704d14094ac4e7bf3b0d5 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Tue, 2 Nov 2021 16:39:20 -0700 Subject: Ch137 (Clojure): prep for challenge --- challenge-137/tyler-wardhaugh/clojure/README.md | 12 ++++++------ challenge-137/tyler-wardhaugh/clojure/bb.edn | 4 +--- challenge-137/tyler-wardhaugh/clojure/deps.edn | 3 +-- challenge-137/tyler-wardhaugh/clojure/pom.xml | 8 ++++---- .../tyler-wardhaugh/clojure/src/tw/weekly/c137/core.clj | 12 ++++++++++++ .../tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj | 12 ++++++++++++ .../tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj | 13 +++++++++++++ .../tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj | 6 ++++++ .../tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj | 6 ++++++ 9 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/core.clj create mode 100644 challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj create mode 100644 challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj create mode 100644 challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj create mode 100644 challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj diff --git a/challenge-137/tyler-wardhaugh/clojure/README.md b/challenge-137/tyler-wardhaugh/clojure/README.md index d05825bedb..371f142e4e 100644 --- a/challenge-137/tyler-wardhaugh/clojure/README.md +++ b/challenge-137/tyler-wardhaugh/clojure/README.md @@ -1,7 +1,7 @@ -# tw.weekly.c136 +# tw.weekly.c137 -The Weekly Challenge - #136 - Tyler Wardhaugh +The Weekly Challenge - #137 - 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.c136.core + $ clojure -M -m tw.weekly.c137.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.c136.t1 M N + $ clojure -M -m tw.weekly.c137.t1 # ... or ... - $ bb run task-1 M N + $ bb run task-1 Run Task #2 with input: - $ clojure -M -m tw.weekly.c136.t2 N + $ clojure -M -m tw.weekly.c137.t2 N # ... or ... $ bb run task-2 N diff --git a/challenge-137/tyler-wardhaugh/clojure/bb.edn b/challenge-137/tyler-wardhaugh/clojure/bb.edn index 2299ced5c9..69331331eb 100644 --- a/challenge-137/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-137/tyler-wardhaugh/clojure/bb.edn @@ -70,9 +70,7 @@ :task (run-task :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (binding [*out* *err*] - (println "error: can't run Task 2 via Babashka because it depends on some incompatible libraries.") - (System/exit 1))} + :task (run-task-bb :t2 *command-line-args*)} both {:doc "Run both tasks (via clojure)" :task (do diff --git a/challenge-137/tyler-wardhaugh/clojure/deps.edn b/challenge-137/tyler-wardhaugh/clojure/deps.edn index 99b46a9e15..5b1400b27e 100644 --- a/challenge-137/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-137/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,5 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"} - org.clojure/math.combinatorics {:mvn/version "0.1.6"}} + :deps {org.clojure/clojure {:mvn/version "1.10.3"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.0"} diff --git a/challenge-137/tyler-wardhaugh/clojure/pom.xml b/challenge-137/tyler-wardhaugh/clojure/pom.xml index fbbe52d202..03a691f300 100644 --- a/challenge-137/tyler-wardhaugh/clojure/pom.xml +++ b/challenge-137/tyler-wardhaugh/clojure/pom.xml @@ -2,11 +2,11 @@ 4.0.0 tw.weekly - tw.weekly.c136 + tw.weekly.c137 0.1.0-SNAPSHOT - tw.weekly.c136 - Challenge #136 - https://github.com/tw.weekly/tw.weekly.c136 + tw.weekly.c137 + Challenge #137 + https://github.com/tw.weekly/tw.weekly.c137 Eclipse Public License diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/core.clj b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/core.clj new file mode 100644 index 0000000000..53ca78eb2c --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c137.core + (:require [tw.weekly.c137.t1 :as t1]) + (:require [tw.weekly.c137.t2 :as t2]) + (:gen-class)) + +(defn -main + "Run all tasks" + [& _] + (println "Task #1:") + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj new file mode 100644 index 0000000000..167c4a8269 --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c137.t1 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #1 › Long Year +;;; + + +(defn -main + "Run Task 1 with a given input M and N, defaulting to the first example from + the task description." + [& args]) diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj new file mode 100644 index 0000000000..8b7cf78fc2 --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj @@ -0,0 +1,13 @@ +(ns tw.weekly.c137.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2 › Lychrel Number +;;; +(def DEFAULT-INPUT [56]) + +(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)])) diff --git a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj new file mode 100644 index 0000000000..ddbab3e1c6 --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj @@ -0,0 +1,6 @@ +(ns tw.weekly.c137.t1-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c137.t1 :refer []])) + +(deftest examples + (testing "Examples from description")) diff --git a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj new file mode 100644 index 0000000000..04d601a7d2 --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj @@ -0,0 +1,6 @@ +(ns tw.weekly.c137.t2-test + (:require [clojure.test :refer [deftest is testing]] + [tw.weekly.c137.t2 :refer []])) + +(deftest examples + (testing "Examples from description")) -- cgit From a3d286ab553679fdb1405ee8ffc7e4b8761823a6 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Fri, 5 Nov 2021 15:55:59 -0700 Subject: Ch137 (Clojure): Task 1 --- challenge-137/tyler-wardhaugh/clojure/bb.edn | 18 +++++---- .../clojure/src/tw/weekly/c137/t1.clj | 12 ------ .../clojure/src/tw/weekly/c137/t1.cljc | 44 ++++++++++++++++++++++ .../clojure/test/tw/weekly/c137/t1_test.clj | 10 ++++- 4 files changed, 62 insertions(+), 22 deletions(-) delete mode 100644 challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj create mode 100644 challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc diff --git a/challenge-137/tyler-wardhaugh/clojure/bb.edn b/challenge-137/tyler-wardhaugh/clojure/bb.edn index 69331331eb..f0892047d3 100644 --- a/challenge-137/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-137/tyler-wardhaugh/clojure/bb.edn @@ -21,14 +21,16 @@ (-> file get-first-form second str))) (defn run-task - [task args] - (let [clj-options (format "-M -m %s " (get-task-ns task))] - (apply clojure clj-options args))) + ([task args] (run-task task args (get-task-ns task))) + ([task args task-ns] + (let [clj-options (format "-M -m %s " task-ns)] + (apply clojure clj-options args)))) (defn run-task-bb - [task args] - (let [bb-cmd (format "bb -m %s " (get-task-ns task))] - (apply shell bb-cmd args)))) + ([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))))) clean {:doc "Clean out temporary files" :task (run! fs/delete-tree @@ -61,10 +63,10 @@ c**** {:doc "CHALLENGE TASKS"} task-1 {:doc "Run Task 1 (via clojure)" - :task (run-task :t1 *command-line-args*)} + :task (run-task :t1 *command-line-args* "tw.weekly.c137.t1")} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1 *command-line-args*)} + :task (run-task-bb :t1 *command-line-args* "tw.weekly.c137.t1")} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task :t2 *command-line-args*)} diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj deleted file mode 100644 index 167c4a8269..0000000000 --- a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.clj +++ /dev/null @@ -1,12 +0,0 @@ -(ns tw.weekly.c137.t1 - (:require [clojure.edn :as edn])) - -;;; -; Task description for TASK #1 › Long Year -;;; - - -(defn -main - "Run Task 1 with a given input M and N, defaulting to the first example from - the task description." - [& args]) diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc new file mode 100644 index 0000000000..a08e5444a9 --- /dev/null +++ b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t1.cljc @@ -0,0 +1,44 @@ +(ns tw.weekly.c137.t1 + (:require [clojure.pprint :refer [cl-format]]) + #?(:bb (:import []) + :clj (:import [java.time LocalDate] + [java.time.temporal WeekFields]))) +;;; +; Task description for TASK #1 › Long Year +;;; + +; define long-year-temporal? +#?(:bb + (defn long-year-temporal? [_] + (throw (Exception. "java.time.temporal not supported on Babashka"))) + + :clj + (let [week-of-year (.weekOfYear (WeekFields/ISO))] + (defn long-year-temporal? + "Is year long? (Uses Java's temporal library to get week number.)" + [year] + (-> (LocalDate/of year 12 28) + (.get week-of-year) + (= 53))))) + +; define long-year-manual? +; source: https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year +(let [p (fn [year] + (-> (+ year + (quot year 4) + (* -1 (quot year 100)) + (quot year 400)) + (mod 7)))] + (defn long-year-manual? + "Is year long? (Uses the method described in Wikipedia.)" + [year] + (or (= (p year) 4) (= (p (dec year)) 3)))) + +; choose an implementation based on our runtime (Clojure JVM or Babashka) +(def long-year? #?(:bb long-year-manual? :clj long-year-temporal?)) + +(defn -main + "Run Task 1 with a given input M and N, defaulting to the first example from + the task description." + [& _] + (cl-format true "~{~a~^, ~}~%" (filter long-year? (range 1900 2101)))) diff --git a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj index ddbab3e1c6..a083ce82f1 100644 --- a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj +++ b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t1_test.clj @@ -1,6 +1,12 @@ (ns tw.weekly.c137.t1-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c137.t1 :refer []])) + [tw.weekly.c137.t1 :refer [long-year-temporal? long-year-manual?]])) (deftest examples - (testing "Examples from description")) + (testing "Examples from description" + (let [source (range 1900 2101)] + (is (= (sequence (filter long-year-temporal?) source) + (sequence (filter long-year-manual?) source) + [1903 1908 1914 1920 1925 1931 1936 1942 1948 1953 1959 1964 1970 + 1976 1981 1987 1992 1998 2004 2009 2015 2020 2026 2032 2037 2043 + 2048 2054 2060 2065 2071 2076 2082 2088 2093 2099]))))) -- cgit From 401f7d3d4e03125f247ff7246d78ed81415e327d Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Fri, 5 Nov 2021 16:33:38 -0700 Subject: Ch137 (Clojure): Task 2 --- .../clojure/src/tw/weekly/c137/t2.clj | 22 ++++++++++++++++++++-- .../clojure/test/tw/weekly/c137/t2_test.clj | 12 ++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj index 8b7cf78fc2..8f288e825c 100644 --- a/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj +++ b/challenge-137/tyler-wardhaugh/clojure/src/tw/weekly/c137/t2.clj @@ -1,13 +1,31 @@ (ns tw.weekly.c137.t2 - (:require [clojure.edn :as edn])) + (:require [clojure.edn :as edn] + [clojure.string :as str])) ;;; ; Task description for TASK #2 › Lychrel Number ;;; (def DEFAULT-INPUT [56]) +(def MAX-TIMES 500) +(def MAX-NUM 10000000) + +(defn flip + [n] + (Integer/parseInt (-> n str str/reverse))) + +(defn lychrel + [n] + (loop [n n + times 1] + (cond + (>= times MAX-TIMES) 1 + (>= n MAX-NUM) 1 + (= n (flip n)) 0 + :else (recur (+ n (flip n)) (inc times))))) (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)])) + (let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (if (lychrel N) 1 0)))) diff --git a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj index 04d601a7d2..d61252832a 100644 --- a/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj +++ b/challenge-137/tyler-wardhaugh/clojure/test/tw/weekly/c137/t2_test.clj @@ -1,6 +1,14 @@ (ns tw.weekly.c137.t2-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c137.t2 :refer []])) + [tw.weekly.c137.t2 :refer [lychrel]])) (deftest examples - (testing "Examples from description")) + (testing "Examples from description" + (is (= 0 (lychrel 56))) + (is (= 0 (lychrel 57))) + (is (= 0 (lychrel 59))))) + +(deftest maximum + (testing "Numbers that exceed our ceiling" + (is (= 1 (lychrel 998))) + (is (= 1 (lychrel 196))))) -- cgit