Project Euler Problem 67

Ivar Thorson bio photo By Ivar Thorson

Problem 67 is just a harder version of problem 18.

This is almost entirely cut-and-paste from problem 18, with the addition of a single function to load the triangle from a file:

(use '[clojure.contrib.duck-streams :only (read-lines)])

(defn read-triangle [file]
  (map (fn [line] (map #(Integer/parseInt %) (re-seq #"\d+" line)))
       (read-lines file)))

(defn merge-rows[a b]
  (map + (map #(apply max %) (partition 2 1 a)) b))

(defn euler-67 [file]
  (reduce merge-rows (reverse (read-triangle file))))

(time (euler-67 "/path/to/triangle.txt")) ;; "Elapsed time: 7.945236 msecs"

Ciao!