module Problem11 where

import Data.List

main :: IO ()
main = do
    contents <- readFile "txt/11.txt"
    let grid = map (map read . words) . lines $ contents
    print $ maximum
        [ maxValHorizontal grid
        , maxValVertical grid
        , maxValDiagonal grid
        , maxValDiagonal2 grid
        ]

maxValHorizontal :: [[Int]] -> Int
maxValHorizontal grid = maximum $ map (maxProd 4) grid

maxProd :: Int -> [Int] -> Int
maxProd n xs
    | len < n   = 0
    | len == n  = product xs
    | otherwise = max (product $ take n xs) (maxProd n $ tail xs)
    where len = length xs

maxValVertical :: [[Int]] -> Int
maxValVertical = maxValHorizontal . transpose

maxValDiagonal :: [[Int]] -> Int
maxValDiagonal grid = maximum
    [ (grid !! i !! j)
      * (grid !! (i + 1) !! (j + 1))
      * (grid !! (i + 2) !! (j + 2))
      * (grid !! (i + 3) !! (j + 3))
    | i <- [0 .. n + 1]
    , i + 3 < n
    , j <- [0 .. n + 1]
    , j + 3 < n
    ]
    where n = length grid -- given rows = cols


maxValDiagonal2 :: [[Int]] -> Int
maxValDiagonal2 = maxValDiagonal . map reverse
