import Data.Char
import Data.List.Split
import Data.List

main :: IO ()
main = do
  input_raw <- map (map digitToInt) <$> lines <$> readFile "day09/input"
  let nines = take (length (head input_raw)) $ repeat 9
  let nineIt x = map ((9:) . (++[9])) $ (nines : x ++ [nines])
  print $ day09 (nineIt input_raw) input_raw

day09 :: [[Int]] -> [[Int]] -> Int
day09 input input_raw = foldl (\acc (x,y) ->if y then acc+1+x else acc) 0 $ zip (concat input_raw) bools
    where vert = tail $ init $ map ((map isSmol) . divvy 3 1) (transpose input)
          horz = tail $ init $ map ((map isSmol) . divvy 3 1) input
          isSmol [a,b,c] = if b<a && b<c then True else False
          isSmol :: [Int] -> Bool
          bools = zipWith (&&) (concat (transpose vert)) (concat horz)
