Monday, February 22, 2016

Project Euler Problem 67 Common Lisp

Project Euler Problem 67

(defparameter *data*
  (with-open-file (input "p067_triangle.txt")
    (loop for line = (read-line input nil nil)
          while line
          collect (read-from-string

                    (concatenate 'string "(" line ")")))))

The above code for converting lines in a text file into a nested list came from here.

(defun bottom-up-max (a b)
  (mapcar #'max
          (mapcar #'+ a b)
          (mapcar #'+ (rest a) b)))

(defun problem67 (lst)
  (first (reduce #'bottom-up-max (reverse lst))))


To get the solution, evaluate:

(problem67 *data*)

No comments:

Post a Comment