1
0
Fork 0
projecteuler/81.hs

17 lines
631 B
Haskell
Raw Normal View History

2017-08-13 18:21:21 +00:00
import Data.List.Split
getInitialLine :: [Integer] -> [Integer]
getInitialLine firstLine = tail $ scanl (+) 0 firstLine
getNextLine :: [Integer] -> [Integer] -> [Integer]
getNextLine prevLine currLine =
tail $ scanl minSum (head prevLine) (zip prevLine currLine)
where minSum currSum (up, left) = min (left + up) (left + currSum)
main :: IO ()
main = do
contents <- readFile "p081_matrix.txt"
let fileLines = lines contents
2017-08-13 19:39:40 +00:00
matrix = map (map read . (splitOn ",")) fileLines
initialLine = getInitialLine $ head matrix
2017-08-13 18:21:21 +00:00
print $ last $ foldl getNextLine initialLine $ tail matrix