Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(use '[clojure.contrib.duck-streams :only (reader read-lines)])
(defn build-sum-mtx [triangle]
(let [len (count triangle)]
(loop [mtx [] i 0]
(cond
(= 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 []
(max-sum
(build-sum-mtx
;; reduce below builds a triangle as a vector of vectors
(reduce
conj []
(map (fn [line]
(vec (map #(Integer. %)
(.split line " "))))
(read-lines (reader "http://projecteuler.net/project/triangle.txt")))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.