• + 0 comments

    Haskell

    module Main where
    
    import qualified Data.Array  as A
    
    type Hourglass = A.Array (Int, Int) Int
    
    inputToHourglass :: String -> Hourglass
    inputToHourglass s = A.listArray ((0, 0), (5, 5)) $ map read $ words s
    
    hsum :: Hourglass -> Int -> Int -> Int
    hsum h x y = sum [h A.! (x', y') | x' <- [x .. x + 2], y' <- [y .. y + 2], (x', y') /= (x + 1, y), (x', y') /= (x + 1, y + 2)]
    
    maxHourglassSum :: Hourglass -> Int
    maxHourglassSum h = maximum [hsum h x y | x <- [0 .. 3], y <- [0 .. 3]]
    
    main :: IO ()
    main = interact $ show . maxHourglassSum . inputToHourglass