aboutsummaryrefslogtreecommitdiff
path: root/challenge-072/tyler-wardhaugh/clojure/src/tw
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-072/tyler-wardhaugh/clojure/src/tw')
-rw-r--r--challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/core.clj12
-rw-r--r--challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj22
-rw-r--r--challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t2.clj18
3 files changed, 52 insertions, 0 deletions
diff --git a/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/core.clj b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/core.clj
new file mode 100644
index 0000000000..b7d09ae991
--- /dev/null
+++ b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/core.clj
@@ -0,0 +1,12 @@
+(ns tw.weekly.c72.core
+ (:require [tw.weekly.c72.t1 :as t1])
+ (:require [tw.weekly.c72.t2 :as t2])
+ (:gen-class))
+
+(defn -main
+ "Run all tasks"
+ [& _]
+ (println "Task #1")
+ (t1/-main)
+ (println "\n\nTask #2")
+ (t2/-main))
diff --git a/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj
new file mode 100644
index 0000000000..daefe5c833
--- /dev/null
+++ b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t1.clj
@@ -0,0 +1,22 @@
+(ns tw.weekly.c72.t1
+ (:require [clojure.pprint :refer [cl-format]])
+ (:require [clojure.edn :as edn]))
+
+(defn fact
+ "Factorial"
+ [n]
+ (->> n inc (range 1) (reduce *)))
+
+(defn trailing-zeros
+ "Count the trailing 0s in a number"
+ [n]
+ (let [[d cnt] (->> n str reverse (partition-by #(= \0 %)) first ((juxt first count)))]
+ (if (= d \0) cnt 0)))
+
+(defn -main
+ "Run Task 1 with a number N, defaulting to 10"
+ [& args]
+ (let [n (or (some-> args first edn/read-string) 10)
+ n! (fact n)
+ zs (trailing-zeros n!)]
+ (cl-format true "~a! = ~a has ~a trailing zero~:p" n n! zs)))
diff --git a/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t2.clj b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t2.clj
new file mode 100644
index 0000000000..072a02ef1e
--- /dev/null
+++ b/challenge-072/tyler-wardhaugh/clojure/src/tw/weekly/c72/t2.clj
@@ -0,0 +1,18 @@
+(ns tw.weekly.c72.t2
+ (:require [clojure.java.io :as io])
+ (:require [clojure.edn :as edn])
+ (:require [pjstadig.reducible-stream :refer [decode-lines!]]))
+
+(defn lines-from
+ "Produce a range of lines from a file"
+ [file a b]
+ (let [xf (comp (drop (dec a)) (take (- b a -1)))
+ source (decode-lines! file)]
+ (sequence xf source)))
+
+(defn -main
+ "Run Task 2 with a file and range of lines to print, defaulting to the first example in the task description"
+ [& args]
+ (let [file (or (some-> args first io/file) (io/resource "input.txt"))
+ [a b] (or (some->> args (drop 1) (take 2) (map edn/read-string)) [4 12])]
+ (dorun (map println (lines-from file a b)))))