re: Daily Challenge #77 - Bird Mountain VIEW POST

FULL DISCUSSION
 

Clojure solution:

;; These could be improved into a macro but want readability
(defn check-left [row col mountains]
  "Checks the cell to the left"
  (if (and (>= (- col 1) 0) 
           (.equals ((mountains row) (- col 1)) "^"))
    true
    false))
(defn check-up [row col mountains]
  "Checks the cell above"
  (if (and (>= (- row 1) 0) 
           (.equals ((mountains (- row 1)) col) "^"))
    true
    false))
(defn check-right [row col mountains]
  "Checks the cell to the right"
  (if (and (< (+ col 1) (count (mountains row))) 
           (.equals ((mountains row) (+ col 1)) "^"))
    true
    false))
(defn check-down [row col mountains]
  "Checks the cell below"
  (if (and (< (+ row 1) (count mountains)) 
           (.equals ((mountains (+ row 1)) col) "^"))
    true
    false))

(defn will-erode? [row col mountains]
  "Returns true if the current cell is not completely surrounded by mountains"
  (not 
   (and (check-left row col mountains)
        (check-up row col mountains)
        (check-right row col mountains)
        (check-down row col mountains))))

(defn erode-mountains [mountains errosion-symbol]
  "Erodes the mountains once"
  (into [] 
        (map-indexed
         (fn [row-index row]
           (into [] 
                 (map-indexed
                  (fn [col-index col]
                    (if (and (.equals col "^")
                             (will-erode? row-index col-index mountains))
                      errosion-symbol
                      col))
                  row)))
         mountains)))

(defn fully-eroded? [mountains]
  (nil? 
    (first (filter #(.equals "^" %)
                   (flatten mountains)))))

(defn peak-height [mountains]
  "Get the peak height (according to a fictional bird)"
  (loop [times 0
         mts mountains]
    (if-not (fully-eroded? mts)
        (recur (+ times 1) (erode-mountains mts times))
    times)))

(def mountains [
  [ "^" "^" "^" "^" "^" "^" " " " " " " " " " " " " " " " " ]
  [ " " "^" "^" "^" "^" "^" "^" "^" "^" " " " " " " " " " " ]
  [ " " " " "^" "^" "^" "^" "^" "^" "^" " " " " " " " " " " ]
  [ " " " " "^" "^" "^" "^" "^" " " " " " " " " " " " " " " ]
  [ " " " " "^" "^" "^" "^" "^" "^" "^" "^" "^" "^" "^" " " ]
  [ " " " " "^" "^" "^" "^" "^" "^" " " " " " " " " " " " " ]
  [ " " " " "^" "^" "^" "^" " " " " " " " " " " " " " " " " ]
])

(peak-height mountains)
;; 3
code of conduct - report abuse