aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj24
-rw-r--r--challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj10
2 files changed, 28 insertions, 6 deletions
diff --git a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj
index 48a8519f75..00df46cf0f 100644
--- a/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj
+++ b/challenge-117/tyler-wardhaugh/clojure/src/tw/weekly/c117/t2.clj
@@ -1,18 +1,34 @@
(ns tw.weekly.c117.t2
- (:require [clojure.edn :as edn]))
+ (:require [clojure.edn :as edn]
+ [clojure.pprint :refer [cl-format]]))
;;;
; Task description for TASK #2 › Find Possible Paths
;;;
(def DEFAULT-INPUT [2])
+(defn- paths
+ "Build up the results for all paths."
+ [r l h n curr results]
+ (if (= (+ r l) (+ r h) n)
+ (conj results curr)
+ (cond-> results
+ (< (+ r l) n) (concat (paths (inc r) l h n (conj curr :R) results)
+ (paths r (inc l) h n (conj curr :L) results))
+ (< h l) (concat (paths r l (inc h) n (conj curr :H) results)))))
+
(defn find-possible-paths
- ""
- [n])
+ "Find all possible paths from top to the bottom right corner."
+ [n]
+ (let [results (paths 0 0 0 n [] [])]
+ (into
+ (sorted-set)
+ (map (fn [coll] (transduce (map name) str "" coll)))
+ results)))
(defn -main
"Run Task 2 with a given input N, defaulting to the first example from the
task description."
[& args]
(let [[N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)]
- (println (find-possible-paths N))))
+ (cl-format true "~{~a~^ ~}~%" (find-possible-paths N))))
diff --git a/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj
index d9547ea900..d59231653f 100644
--- a/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj
+++ b/challenge-117/tyler-wardhaugh/clojure/test/tw/weekly/c117_test.clj
@@ -2,7 +2,7 @@
(:require [clojure.test :refer [deftest is testing]]
[clojure.java.io :as io]
[tw.weekly.c117.t1 :as t1]
- #_[tw.weekly.c117.t2 :refer [find-possible-paths]]))
+ [tw.weekly.c117.t2 :refer [find-possible-paths]]))
(deftest task-1
(testing "Task 1, Missing Row"
@@ -18,4 +18,10 @@
(deftest task-2
(testing "Task 2, Find Possible Paths"
- ))
+ (is (= #{"LH" "R"} (find-possible-paths 1)))
+ (is (= #{"LHLH" "LHR" "LLHH" "LRH" "RLH" "RR"} (find-possible-paths 2)))
+ (is (=
+ #{"LHLHLH" "LHLHR" "LHLLHH" "LHLRH" "LHRLH" "LHRR" "LLHHLH" "LLHHR"
+ "LLHLHH" "LLHRH" "LLLHHH" "LLRHH" "LRHLH" "LRHR" "LRLHH" "LRRH"
+ "RLHLH" "RLHR" "RLLHH" "RLRH" "RRLH" "RRR"}
+ (find-possible-paths 3)))))