Project Euler Problem 082
import Data.List import System.IO.Unsafe ss :: [[Integer]] ss = map (\x -> read ("["++x++"]")::[Integer]) $ lines $ unsafePerformIO $ readFile "082matrix.txt" initss :: [a] -> [[a]] initss = tail . inits cands :: [Integer] -> [Integer] -> [[Integer]] cands acc rights= zipWith (:) acc (initss rights) mcand :: [Integer] -> [Integer] -> Integer mcand a r = minimum $ map sum $ cands a r min3 :: Integer -> Integer -> Integer -> Integer min3 = ((min.).min) :: Integer -> Integer -> Integer -> Integer sol :: [Integer] -> [Integer] -> [Integer] sol aa@(a:b:bs) bb@(x:y:ys)= v1 : imp ([a],b,bs) ([x],y,ys) where v1 = mcand aa bb imp :: ([Integer],Integer,[Integer]) ->([Integer],Integer,[Integer]) ->[Integer] imp (al,up,[]) (l,x,[]) = [val] where val = x + (min up (mcand al l)) imp (al,up,[a]) (l,x,[r]) = val : imp (up:al,a,[]) (x:l,r,[]) where val = x + (min3 up (mcand al l) (a+r)) imp (al,up,(a:ar)) (l,x,(r:rs)) = val : imp (up:al,a,ar) (x:l,r,rs) where val = x + (min3 up (mcand al l) (mcand (a:ar) (r:rs))) ans1 :: Integer ans1 = minimum $ foldl1 sol (transpose ss) -- 260324 main :: IO () main = print ans1