1
0
Fork 0
projecteuler/81.hs

17 lines
646 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
let matrix = map (map read . (splitOn ",")) fileLines :: [[Integer]]
let initialLine = getInitialLine $ head matrix
print $ last $ foldl getNextLine initialLine $ tail matrix