diff options
Diffstat (limited to 'challenge-079/tyler-wardhaugh/clojure/src')
3 files changed, 100 insertions, 0 deletions
diff --git a/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/core.clj b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/core.clj new file mode 100644 index 0000000000..0327a880fc --- /dev/null +++ b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/core.clj @@ -0,0 +1,12 @@ +(ns tw.weekly.c79.core + (:require [tw.weekly.c79.t1 :as t1]) + (:require [tw.weekly.c79.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-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj new file mode 100644 index 0000000000..47140780d4 --- /dev/null +++ b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj @@ -0,0 +1,29 @@ +(ns tw.weekly.c79.t1 + (:require [clojure.edn :as edn])) + +;;; Task description for TASK #1 › Count Set Bits +;You are given a positive number $N. +; +;Write a script to count the total numbrer of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007. +;;;; + +(defn count-bits-to-n + "Count the '1' bits in the binary representation of all the integers [1..n]" + [n] + (let [xf (map #(Integer/bitCount %)) + source (range 1 (inc n))] + (transduce xf + source))) + +(def big-num "The large number given in the task description to modulo against" + 1000000007N) + +(defn run + "Count the 1s and modulo" + [n] + (mod (count-bits-to-n n) big-num)) + +(defn -main + "Run Task 1 with a number N, defaulting to the first one given in the examples" + [& args] + (let [N (or (-> args first edn/read-string) 4)] + (format "%d" (biginteger (run N))))) diff --git a/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t2.clj b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t2.clj new file mode 100644 index 0000000000..45513901c1 --- /dev/null +++ b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t2.clj @@ -0,0 +1,59 @@ +(ns tw.weekly.c79.t2 + (:require [clojure.edn :as edn]) + (:require [oz.core :as oz])) + +;;; Task description for TASK #2 › Trapped Rain Water +; You are given an array of positive numbers @N. +; +; Write a script to represent it as Histogram Chart and find out how much water it can trap. +; +; Example 1: +; Input: @N = (2, 1, 4, 1, 2, 5) +; The histogram representation of the given array is as below. +; 5 # +; 4 # # +; 3 # # +; 2 # # # # +; 1 # # # # # # +; _ _ _ _ _ _ _ +; 2 1 4 1 2 5 +;;; + +(defn splits + "Partition the input by splitting it at each index, ensuring partitions contain at least one element." + [coll] + (map #(split-at % coll) (range 1 (dec (count coll))))) + +(defn calculate-pools + "Returns a sequence of pools, which are the amount of water 'units' that can be held at the inner elements of the coll, when viewed as a histogram." + [coll] + (let [neighboring-maxes (fn [[l [c & r]]] [(apply max l) c (apply max r)]) + to-water (fn [[l c r]] (max (- (min l r) c) 0))] + (->> (splits coll) + (eduction (map neighboring-maxes) (map to-water))))) + +(defn capacity + "Calculates the total capacity of the pools" + [pools] + (reduce + 0 pools)) + +(defn draw-histogram + "Vizualize the histogram, using Oz (which uses Vega-Lite & Vega)" + ([coll] (draw-histogram coll (calculate-pools coll))) + ([coll pools] (let [make-hashes (fn [x land water] (list (hash-map :x x :y land :c "green") (hash-map :x x :y water :c "blue"))) + values (mapcat make-hashes (range) coll (concat [0] pools [0]))] + (oz/view! {:data {:values values} + :encoding {:x {:field :x :type "ordinal"} + :y {:field :y :type "quantitative" :aggregate "sum"} + :color {:field :c + :type "nominal" + :scale nil}} + :mark "bar"})))) + +(defn -main + [& args] + (let [N (or (some->> args (map edn/read-string)) [2 1 4 1 2 5]) + pools (calculate-pools N)] + (println (capacity pools)) + (oz/start-server!) + (draw-histogram N pools))) |
