diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-09-09 08:25:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-09 08:25:24 +0100 |
| commit | 3ab625bbf6806e47ba4e2884d4ce28a1849bd781 (patch) | |
| tree | 295da701264b6a9854ec0b8b4820471723f5d92b /challenge-128 | |
| parent | 6d6b03f430c143a7043c9d4abb3877dd937d9cd9 (diff) | |
| parent | c7f63b645f28c795181ae1c2ba74bfc285385d6d (diff) | |
| download | perlweeklychallenge-club-3ab625bbf6806e47ba4e2884d4ce28a1849bd781.tar.gz perlweeklychallenge-club-3ab625bbf6806e47ba4e2884d4ce28a1849bd781.tar.bz2 perlweeklychallenge-club-3ab625bbf6806e47ba4e2884d4ce28a1849bd781.zip | |
Merge pull request #4854 from tylerw/tw/challenge-128
Ch128 (Clojure): Task 2
Diffstat (limited to 'challenge-128')
3 files changed, 48 insertions, 3 deletions
diff --git a/challenge-128/tyler-wardhaugh/clojure/bb.edn b/challenge-128/tyler-wardhaugh/clojure/bb.edn index 70feb7d8ea..fd7e727c03 100644 --- a/challenge-128/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-128/tyler-wardhaugh/clojure/bb.edn @@ -71,7 +71,9 @@ :task (run-task :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (run-task-bb :t2 *command-line-args*)} + :task (binding [*out* *err*] + (println "error: can't run Task 2 via Babashka because it depends on some unsupported functions.") + (System/exit 1))} both {:doc "Run both tasks (via clojure)" :task (do diff --git a/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t2.clj b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t2.clj new file mode 100644 index 0000000000..2a080cc3be --- /dev/null +++ b/challenge-128/tyler-wardhaugh/clojure/src/tw/weekly/c128/t2.clj @@ -0,0 +1,40 @@ +(ns tw.weekly.c128.t2 + (:require [clojure.edn :as edn])) + +;;; +; Task description for TASK #2, Minimum Platforms +;;; +(def DEFAULT-INPUT [["11:20" "14:30"] ["11:50" "15:00"]]) + +(def MINUTES-PER-DAY (* 24 60)) +(def TIME-REGEX (re-pattern #"([0-2]\d):(\d{2})")) + +(defn time->num + [t] + (let [[[_ h m]] (re-seq TIME-REGEX t)] + (+ (* (Integer/parseInt h) 60) (Integer/parseInt m)))) + +(defn calc-trains-by-minute [arrivals departures] + (let [source (map vector arrivals departures) + xf (comp + (map #(map time->num %)) + (mapcat (fn [[arr dep]] + (if (<= arr dep) + (range arr dep) + (concat (range dep) + (range arr (dec MINUTES-PER-DAY))))))) + f (completing (fn [minutes i] + (doto minutes (aset i (inc (aget minutes i))))))] + (transduce xf f (int-array MINUTES-PER-DAY) source))) + +(defn find-minimum-platforms + [arrivals departures] + (->> (calc-trains-by-minute arrivals departures) + (reduce max))) + +(defn -main + "Run Task 2 with a given input, defaulting to the first example from the + task description." + [& args] + (let [[A D] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] + (println (find-minimum-platforms A D)))) diff --git a/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj b/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj index 940922fab4..89330a2e86 100644 --- a/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj +++ b/challenge-128/tyler-wardhaugh/clojure/test/tw/weekly/c128_test.clj @@ -2,7 +2,7 @@ (:require [clojure.test :refer [deftest is testing]] [clojure.java.io :as io] [tw.weekly.c128.t1 :as t1] - #_[tw.weekly.c128.t2 :as t2])) + [tw.weekly.c128.t2 :refer [find-minimum-platforms]])) (defn solve-t1 [f] @@ -15,4 +15,7 @@ (deftest task-2 (testing "Task 2, Minimum Platforms" - )) + (is (= 1 (find-minimum-platforms ["11:20" "14:30"] ["11:50" "15:00"]))) + (is (= 3 (find-minimum-platforms + ["10:20" "11:00" "11:10" "12:20" "16:20" "19:00"] + ["10:30" "13:20" "12:40" "12:50" "20:20" "21:20"]))))) |
