Skip to content

Instantly share code, notes, and snippets.

@kapilreddy
Created January 15, 2014 13:20
Show Gist options
  • Save kapilreddy/8436055 to your computer and use it in GitHub Desktop.
Save kapilreddy/8436055 to your computer and use it in GitHub Desktop.
(defn divide-coll
"Divide collection of block heights into regions where water will be blocked.
ex.
[1 2 0 1 3 2 1 0 3] -> [[3 2 1 0 3] [2 0 1 3]]"
[c]
(let [regions (reduce (fn [coll i]
(cond
(seq coll) (let [[x & xs] coll
from (first x)
new-x (conj (vec x) i)]
(if (> i from)
(cons [i] (cons new-x xs))
(cons new-x xs)))
:else [[i]]))
[]
c)]
(filter #(> (count %) 2) regions)))
(defn calc-water-coll
"Calculate number of blocks where water will be collected for a sequence of block heights.
ex.
[1 2 0 1 3 2 1 0 3] -> 9"
[c]
(apply +
(map (fn [xs]
(let [x (min (first xs)
(last xs))
xs-len (count xs)]
(apply + (map (fn [i]
(- x i))
(subvec xs 1 (dec xs-len))))))
(divide-coll c))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment