Let's write β

プログラミング中にできたことか、思ったこととか

Project Euler 67をHaskellで

module Main where

import Data.List.Split

readTriangle :: FilePath -> IO [[Int]]
readTriangle fpath = do
  content <- readFile fpath
  return $ map (map read . splitOn " ") $ lines content

prob67 :: [[Int]] -> Int
prob67 = head . prob67'
  where
    prob67' (x:[]) = zipWith max x (tail x)
    prob67' (x:xs) = let y = prob67' xs;
                         newX = zipWith (+) x y
                     in
                      if (length newX) == 1
                      then newX
                      else zipWith max newX (tail newX) 
     
main = do
  triangle <- readTriangle "triangle.txt"
  print (prob67 triangle)