Skip to content

Instantly share code, notes, and snippets.

Created November 16, 2010 05:20
What would you like to do?
(use '[ :only (reader read-lines)])
(defn build-sum-mtx [triangle]
(let [len (count triangle)]
(loop [mtx [] i 0]
(= i len) mtx
:else (let [row (get mtx (dec i))
sum (fn [n j]
(let [a (or (get row (dec j)) 0)
b (or (get row j) 0)]
(+ (max a b) n)))]
(recur (conj mtx
;; converting back lazyseq from map to a vector
;; since we would access it (on line 8) in the next iteration
(vec (map sum (get triangle i) (range 0 (inc i)))))
(inc i)))))))
(defn max-sum [mtx] (apply max (last mtx)))
(defn problem-67 []
;; reduce below builds a triangle as a vector of vectors
conj []
(map (fn [line]
(vec (map #(Integer. %)
(.split line " "))))
(read-lines (reader "")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment