aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-06 10:49:05 +0000
committerGitHub <noreply@github.com>2022-02-06 10:49:05 +0000
commit57392de99b9520be1dd96f172f518393cd1c6cd7 (patch)
tree0eb74f1a5387e4c246090e3737e795ffa4fa689d
parent959912319143bc3f8cd2e029a688818aed94e58d (diff)
parent40cd181ea9567e8fed9fef944e9a8546a325937e (diff)
downloadperlweeklychallenge-club-57392de99b9520be1dd96f172f518393cd1c6cd7.tar.gz
perlweeklychallenge-club-57392de99b9520be1dd96f172f518393cd1c6cd7.tar.bz2
perlweeklychallenge-club-57392de99b9520be1dd96f172f518393cd1c6cd7.zip
Merge pull request #5614 from tylerw/tw/challenge-150
Challenge 150
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/README.md11
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/bb.edn2
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/build.clj19
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/deps.edn6
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/src/c150/t1.clj22
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/src/c150/t2.clj22
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/test/c150/t1_test.clj7
-rw-r--r--challenge-150/tyler-wardhaugh/clojure/test/c150/t2_test.clj30
8 files changed, 111 insertions, 8 deletions
diff --git a/challenge-150/tyler-wardhaugh/clojure/README.md b/challenge-150/tyler-wardhaugh/clojure/README.md
index ad07e31114..69836da026 100644
--- a/challenge-150/tyler-wardhaugh/clojure/README.md
+++ b/challenge-150/tyler-wardhaugh/clojure/README.md
@@ -1,6 +1,6 @@
# c148
-The Weekly Challenge — #148 — Tyler Wardhaugh
+The Weekly Challenge — #150 — Tyler Wardhaugh
## Usage
@@ -8,12 +8,12 @@ Clojure ([installation instructions](https://clojure.org/guides/getting_started#
Run Task #1:
- $ clojure -M:t1
+ $ clojure -M:t1 A B
# ... or ...
- $ bb run task-1
+ $ bb run task-1 A B
# Alternatively, to run it via Babashka:
- $ bb run task-1-bb
+ $ bb run task-1-bb A B
Run Task #2:
@@ -21,6 +21,9 @@ Run Task #2:
# ... or ...
$ bb run task-2
+ # Alternatively, to run it via Babashka:
+ $ bb run task-2-bb
+
Run the project's tests (which are samples from the task descriptions):
$ clojure -T:build test
diff --git a/challenge-150/tyler-wardhaugh/clojure/bb.edn b/challenge-150/tyler-wardhaugh/clojure/bb.edn
index 8e5e3b49c6..e21cd63a1e 100644
--- a/challenge-150/tyler-wardhaugh/clojure/bb.edn
+++ b/challenge-150/tyler-wardhaugh/clojure/bb.edn
@@ -78,6 +78,6 @@
:task (run-task-clj :t2 *command-line-args*)}
task-2-bb {:doc "Run Task 2 (via Babashka)"
- :task (bb-no-go :t2 *command-line-args*)}
+ :task (run-task-bb :t2 *command-line-args*)}
}
}
diff --git a/challenge-150/tyler-wardhaugh/clojure/build.clj b/challenge-150/tyler-wardhaugh/clojure/build.clj
new file mode 100644
index 0000000000..dc6d70c302
--- /dev/null
+++ b/challenge-150/tyler-wardhaugh/clojure/build.clj
@@ -0,0 +1,19 @@
+(ns build
+ (:refer-clojure :exclude [test])
+ (:require [org.corfield.build :as bb]))
+
+(def lib 'net.clojars.c150/c150)
+(def version "0.1.0-SNAPSHOT")
+(def main 'c150.c150)
+
+(defn test "Run the tests." [opts]
+ (bb/run-tests opts))
+
+(def clean bb/clean)
+
+(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts]
+ (-> opts
+ (assoc :lib lib :version version :main main)
+ (bb/run-tests)
+ (bb/clean)
+ (bb/uber)))
diff --git a/challenge-150/tyler-wardhaugh/clojure/deps.edn b/challenge-150/tyler-wardhaugh/clojure/deps.edn
index a58ebaa80d..f1e8a6e0ee 100644
--- a/challenge-150/tyler-wardhaugh/clojure/deps.edn
+++ b/challenge-150/tyler-wardhaugh/clojure/deps.edn
@@ -1,9 +1,9 @@
{:paths ["src" "resources"]
- :deps {org.clojure/clojure {:mvn/version "1.11.0-alpha4"}
+ :deps {org.clojure/clojure {:mvn/version "1.11.0-beta1"}
com.hypirion/primes {:mvn/version "0.2.2"}}
:aliases
- {:t1 {:main-opts ["-m" "c148.t1"]}
- :t2 {:main-opts ["-m" "c148.t2"]}
+ {:t1 {:main-opts ["-m" "c150.t1"]}
+ :t2 {:main-opts ["-m" "c150.t2"]}
:build {:deps {io.github.seancorfield/build-clj
{:git/tag "v0.6.3" :git/sha "9b8e09b"
;; since we're building an app uberjar, we do not
diff --git a/challenge-150/tyler-wardhaugh/clojure/src/c150/t1.clj b/challenge-150/tyler-wardhaugh/clojure/src/c150/t1.clj
new file mode 100644
index 0000000000..74fb87c7fc
--- /dev/null
+++ b/challenge-150/tyler-wardhaugh/clojure/src/c150/t1.clj
@@ -0,0 +1,22 @@
+(ns c150.t1)
+
+(def DEFAULT-INPUT ["1234" "5678"])
+(def SIZE 51)
+
+(defn fibo-words
+ [a b]
+ (let [p (promise)]
+ @(doto p
+ (deliver (list* a b (lazy-seq (map str @p (rest @p))))))))
+
+(defn solve
+ [source]
+ (let [xf (comp (drop-while #(< (count %) SIZE)) (take 1))
+ f (completing (fn [_ w] (nth w (dec SIZE))))]
+ (->> source
+ (transduce xf f nil))))
+
+(defn -main
+ [& args]
+ (let [[A B] (or args DEFAULT-INPUT)]
+ (println (solve (fibo-words A B)))))
diff --git a/challenge-150/tyler-wardhaugh/clojure/src/c150/t2.clj b/challenge-150/tyler-wardhaugh/clojure/src/c150/t2.clj
new file mode 100644
index 0000000000..c982dd76d2
--- /dev/null
+++ b/challenge-150/tyler-wardhaugh/clojure/src/c150/t2.clj
@@ -0,0 +1,22 @@
+(ns c150.t2
+ (:require [clojure.math :as m]
+ [clojure.pprint :refer [cl-format]]))
+
+(def TARGET 500)
+
+(defn prime-squares-upto
+ [ceiling]
+ (let [source (iterate #(.nextProbablePrime %) BigInteger/ONE)
+ xf (comp (drop 1) (take-while #(< % ceiling)) (map #(m/pow % 2)))]
+ (into [] xf source)))
+
+(defn square-free-ints
+ [n]
+ (let [prime-squares (prime-squares-upto (m/sqrt n))
+ square-free? (fn [x] (not-any? #(zero? (rem x %)) prime-squares))]
+ (->> (range 1 (inc n))
+ (filter square-free?))))
+
+(defn -main
+ [& _]
+ (cl-format true "~{~<~%~1,78:;~A~;~^,~> ~}" (square-free-ints TARGET)))
diff --git a/challenge-150/tyler-wardhaugh/clojure/test/c150/t1_test.clj b/challenge-150/tyler-wardhaugh/clojure/test/c150/t1_test.clj
new file mode 100644
index 0000000000..70ad696926
--- /dev/null
+++ b/challenge-150/tyler-wardhaugh/clojure/test/c150/t1_test.clj
@@ -0,0 +1,7 @@
+(ns c150.t1-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c150.t1 :refer [DEFAULT-INPUT fibo-words solve]]))
+
+(deftest examples
+ (testing "Examples from the test description"
+ (is (= \7 (solve (apply fibo-words DEFAULT-INPUT))))))
diff --git a/challenge-150/tyler-wardhaugh/clojure/test/c150/t2_test.clj b/challenge-150/tyler-wardhaugh/clojure/test/c150/t2_test.clj
new file mode 100644
index 0000000000..8f59be9950
--- /dev/null
+++ b/challenge-150/tyler-wardhaugh/clojure/test/c150/t2_test.clj
@@ -0,0 +1,30 @@
+(ns c150.t2-test
+ (:require [clojure.test :refer [deftest is testing]]
+ [c150.t2 :refer [square-free-ints]]))
+
+(def FIRST-500-SQUARE-FREE-INTS
+ [1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31,
+ 33, 34, 35, 37, 38, 39, 41, 42, 43, 46, 47, 51, 53, 55, 57, 58, 59, 61,
+ 62, 65, 66, 67, 69, 70, 71, 73, 74, 77, 78, 79, 82, 83, 85, 86, 87, 89,
+ 91, 93, 94, 95, 97, 101, 102, 103, 105, 106, 107, 109, 110, 111, 113,
+ 114, 115, 118, 119, 122, 123, 127, 129, 130, 131, 133, 134, 137, 138,
+ 139, 141, 142, 143, 145, 146, 149, 151, 154, 155, 157, 158, 159, 161,
+ 163, 165, 166, 167, 170, 173, 174, 177, 178, 179, 181, 182, 183, 185,
+ 186, 187, 190, 191, 193, 194, 195, 197, 199, 201, 202, 203, 205, 206,
+ 209, 210, 211, 213, 214, 215, 217, 218, 219, 221, 222, 223, 226, 227,
+ 229, 230, 231, 233, 235, 237, 238, 239, 241, 246, 247, 249, 251, 253,
+ 254, 255, 257, 258, 259, 262, 263, 265, 266, 267, 269, 271, 273, 274,
+ 277, 278, 281, 282, 283, 285, 286, 287, 290, 291, 293, 295, 298, 299,
+ 301, 302, 303, 305, 307, 309, 310, 311, 313, 314, 317, 318, 319, 321,
+ 322, 323, 326, 327, 329, 330, 331, 334, 335, 337, 339, 341, 345, 346,
+ 347, 349, 353, 354, 355, 357, 358, 359, 362, 365, 366, 367, 370, 371,
+ 373, 374, 377, 379, 381, 382, 383, 385, 386, 389, 390, 391, 393, 394,
+ 395, 397, 398, 399, 401, 402, 403, 406, 407, 409, 410, 411, 413, 415,
+ 417, 418, 419, 421, 422, 426, 427, 429, 430, 431, 433, 434, 435, 437,
+ 438, 439, 442, 443, 445, 446, 447, 449, 451, 453, 454, 455, 457, 458,
+ 461, 462, 463, 465, 466, 467, 469, 470, 471, 473, 474, 478, 479, 481,
+ 482, 483, 485, 487, 489, 491, 493, 494, 497, 498, 499])
+
+(deftest target
+ (testing "Target identified in task description"
+ (is (= FIRST-500-SQUARE-FREE-INTS (square-free-ints 500)))))