{-# LANGUAGE TupleSections #-} import Data.List -- this is literally only a function because read is stupid parse :: String -> [[Int]] parse = map (map (read . singleton)) . lines getGridSize :: [[a]] -> (Int, Int) getGridSize grid = (length $ head grid, length grid) isVisible :: [[Int]] -> (Int, Int) -> Bool isVisible grid (x, y) = any startStep [(-1, 0), (1, 0), (0, -1), (0, 1)] where startGridValue = grid !! y !! x (gridWidth, gridHeight) = getGridSize grid startStep (facingX, facingY) = isVisible' (x + facingX, y + facingY) (facingX, facingY) isVisible' (gridX, gridY) (facingX, facingY) | gridX < 0 || gridY < 0 || gridX > (gridWidth - 1) || gridY > (gridHeight - 1) = True | otherwise = (grid !! gridY !! gridX) < startGridValue && isVisible' (gridX + facingX, gridY + facingY) (facingX, facingY) listPermutations :: [a] -> [a] -> [(a, a)] listPermutations a = concatMap (\b' -> map (, b') a) getAllCoordinates grid = listPermutations [0..gridWidth-1] [0..gridHeight-1] where (gridWidth, gridHeight) = getGridSize grid main = interact $ show . length . filter id . (\grid -> map (isVisible grid) $ getAllCoordinates grid) . parse