From 86de64b235e0caf3191db46a5885550ffbf93036 Mon Sep 17 00:00:00 2001 From: Tyler Wardhaugh Date: Thu, 1 Oct 2020 10:18:49 -0700 Subject: Ch80/Task 2 (clj): improve algorithm --- challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj | 6 ++---- challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj | 5 +++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj b/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj index 8d12ce4e14..55586b34a8 100644 --- a/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj +++ b/challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj @@ -14,10 +14,8 @@ (defn count-candies "Determine the number of candies needed according to the rules in the task description." [coll] - (let [xf (comp (map (juxt (fn [[a b _]] (if (> b a) 1 0)) - (fn [[_ b c]] (if (> b c) 1 0)))) - (map (partial apply + 1))) - source (partition 3 1 (repeat ##Inf) (concat [##Inf] coll))] + (let [xf (map #(inc (if (apply not= %) 1 0))) + source (partition 2 1 (list (last coll)) coll)] (transduce xf + source))) (defn -main diff --git a/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj b/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj index 7c88e71f6b..32251e39f5 100644 --- a/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj +++ b/challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj @@ -14,5 +14,6 @@ (deftest task-2 (testing "Task 2, Count Candies" - (is (= (count-candies [1, 2, 2]) 4)) - (is (= (count-candies [1, 4, 3, 2]) 7)))) + (is (= (count-candies [1 2 2]) 4)) + (is (= (count-candies [1 4 3 2]) 7)) + (is (= (count-candies [2 1 4 3 1 2]) 11)))) -- cgit