|
| 1 | +(ns clojure-practice.random.pied-piper |
| 2 | + (:require [clojure.string :as str])) |
| 3 | + |
| 4 | +(comment |
| 5 | + |
| 6 | + "The Pied Piper has been enlisted to play his magical tune and coax all the rats out of town. |
| 7 | +
|
| 8 | + But some of the rats are deaf and are going the wrong way! |
| 9 | +
|
| 10 | + How many deaf rats are there? |
| 11 | +
|
| 12 | + Legend |
| 13 | +
|
| 14 | + P = The Pied Piper |
| 15 | + O ~ = Rat going left |
| 16 | + ~O = Rat going right |
| 17 | + Example |
| 18 | +
|
| 19 | + ex1 ~O ~O ~O ~O P has 0 deaf rats |
| 20 | + ex2 P O ~ O ~ ~O O ~ has 1 deaf rat |
| 21 | + ex3 ~O ~O ~O ~OP ~O ~OO~ has 2 deaf rats" |
| 22 | + |
| 23 | + ) |
| 24 | + |
| 25 | +(defn rats-count |
| 26 | + [rats-str] |
| 27 | + (loop [rats (partition 2 rats-str) |
| 28 | + left-rats 0 |
| 29 | + right-rats 0] |
| 30 | + ; (println {:left-rats left-rats :right-rats right-rats}) |
| 31 | + (if rats |
| 32 | + (let [rat (first rats)] |
| 33 | + (recur (next rats) |
| 34 | + (if (= (last rat) \~) |
| 35 | + (inc left-rats) |
| 36 | + left-rats) |
| 37 | + (if (= (first rat) \~) |
| 38 | + (inc right-rats) |
| 39 | + right-rats))) |
| 40 | + {:left-rats left-rats |
| 41 | + :right-rats right-rats}))) |
| 42 | + |
| 43 | +#_(defn where-is-piper? |
| 44 | + [input-str] |
| 45 | + (cond (= (first input-str) \P) :left |
| 46 | + (= (last input-str) \P) :right |
| 47 | + :else :random)) |
| 48 | + |
| 49 | +#_(defn deaf-rats-count* |
| 50 | + [rats-count piper-direction] |
| 51 | + (cond (= piper-direction :left) (-> rats-count |
| 52 | + :right-rats) |
| 53 | + (= piper-direction :right) (-> rats-count |
| 54 | + :left-rats))) |
| 55 | +(defn deaf-rats-on-left-of-piper |
| 56 | + [rats-count] |
| 57 | + (-> rats-count |
| 58 | + :left-rats)) |
| 59 | + |
| 60 | +(defn deaf-rats-on-right-of-piper |
| 61 | + [rats-count] |
| 62 | + (-> rats-count |
| 63 | + :right-rats)) |
| 64 | + |
| 65 | + |
| 66 | +(defn deaf-rats-count |
| 67 | + [input] |
| 68 | + (let [input-str-with-no-spaces (str/replace input |
| 69 | + " " |
| 70 | + "") |
| 71 | + rats-left-and-right-of-piper (str/split input-str-with-no-spaces |
| 72 | + #"P" |
| 73 | + -1) |
| 74 | + ;_ (println "rats-on-left-and-right-of-piper: " rats-on-left-and-right-of-piper) |
| 75 | + [rats-count-left-of-piper rats-count-right-of-piper] (mapv rats-count |
| 76 | + rats-left-and-right-of-piper) |
| 77 | + ;_ (println (str rats-count-left-of-piper " P " rats-count-right-of-piper)) |
| 78 | + ] |
| 79 | + (+ (deaf-rats-on-left-of-piper rats-count-left-of-piper) |
| 80 | + (deaf-rats-on-right-of-piper rats-count-right-of-piper)))) |
| 81 | + |
| 82 | + |
| 83 | +;;REPL execution block |
| 84 | +(comment |
| 85 | + (str/split "~O ~O ~O ~O P" #"P" -1) |
| 86 | + #_=> ["~O ~O ~O ~O " ""] |
| 87 | + (str/split "~O ~O ~O ~OP ~O ~OO~" #"P") |
| 88 | + #_=> ["~O ~O ~O ~O" " ~O ~OO~"] |
| 89 | + |
| 90 | + (str/replace "~O ~O ~O ~OP ~O ~OO~" |
| 91 | + " " |
| 92 | + "") |
| 93 | + #_=> "~O~O~O~OP~O~OO~" |
| 94 | + |
| 95 | + (str/replace "~O ~O ~O ~O P" |
| 96 | + " " |
| 97 | + "") |
| 98 | + #_=> "~O~O~O~OP" |
| 99 | + |
| 100 | + (str/replace "P O ~ O ~ ~O O ~" |
| 101 | + " " |
| 102 | + "") |
| 103 | + #_=> "PO~O~~OO~" |
| 104 | + |
| 105 | + ;(where-is-piper? "~O~O~O~OP~O~OO~") |
| 106 | + ;#_=> :random |
| 107 | + ; |
| 108 | + ;(where-is-piper? "~O~O~O~OP") |
| 109 | + ;#_=> :right |
| 110 | + ; |
| 111 | + ;(where-is-piper? "PO~O~~OO~") |
| 112 | + ;#_=> :left |
| 113 | + |
| 114 | + (partition 2 "~O~O~O~O") |
| 115 | + #_=> ((\~ \O) (\~ \O) (\~ \O) (\~ \O)) |
| 116 | + |
| 117 | + (rats-count "~O~O~O~O") |
| 118 | + #_=> {:left-rats 0, :right-rats 4} |
| 119 | + |
| 120 | + (rats-count "~O~O~O~O~O~OO~") |
| 121 | + #_=> {:left-rats 1, :right-rats 6} |
| 122 | + |
| 123 | + (rats-count "O~O~~OO~") |
| 124 | + #_=> {:left-rats 3, :right-rats 1} |
| 125 | + |
| 126 | + (deaf-rats-on-right-of-piper {:left-rats 3, :right-rats 1}) |
| 127 | + #_=> 1 |
| 128 | + |
| 129 | + (deaf-rats-on-left-of-piper {:left-rats 0, :right-rats 0}) |
| 130 | + #_=> 0 |
| 131 | + |
| 132 | + (deaf-rats-count "PO~O~~OO~") |
| 133 | + #_=> 1 |
| 134 | + |
| 135 | + (deaf-rats-count "~O ~O ~O ~O P") |
| 136 | + #_=> 0 |
| 137 | + |
| 138 | + (deaf-rats-count "~O ~O ~O ~OP ~O ~OO~") |
| 139 | + #_=> 2 |
| 140 | + |
| 141 | + (deaf-rats-count "P") |
| 142 | + #_=> 0 |
| 143 | + |
| 144 | + |
| 145 | + ) |
0 commit comments