Created
January 15, 2014 13:20
-
-
Save kapilreddy/8436055 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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