aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-01 10:18:49 -0700
committerTyler Wardhaugh <tyler.wardhaugh@gmail.com>2020-10-01 13:31:05 -0700
commit86de64b235e0caf3191db46a5885550ffbf93036 (patch)
tree27f8a7ad87257a066dbeddaf58c4e159e84fe2b8
parent6ee7fd41be52d089f3aff1a9f211da73565ad35a (diff)
downloadperlweeklychallenge-club-86de64b235e0caf3191db46a5885550ffbf93036.tar.gz
perlweeklychallenge-club-86de64b235e0caf3191db46a5885550ffbf93036.tar.bz2
perlweeklychallenge-club-86de64b235e0caf3191db46a5885550ffbf93036.zip
Ch80/Task 2 (clj): improve algorithm
-rw-r--r--challenge-080/tyler-wardhaugh/clojure/src/tw/weekly/c80/t2.clj6
-rw-r--r--challenge-080/tyler-wardhaugh/clojure/test/tw/weekly/c80_test.clj5
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))))