module Util where import Data.Char (isSpace) import qualified Data.Map as Map import Data.Map (Map) splitOn :: (a -> Bool) -> [a] -> [[a]] splitOn p s = case dropWhile p s of [] -> [] s' -> w : splitOn p s'' where (w, s'') = break p s' split :: (Eq a) => a -> [a] -> [[a]] split c = splitOn (== c) enumerate :: [a] -> [(Int,a)] enumerate l = zip [0..] l type Point2 = (Int,Int) type Point3 = (Int,Int,Int) type Grid = Map Point2 Char toGrid :: String -> Grid toGrid s = Map.fromList [((x,y),c) | (y,r) <- enumerate (lines s), (x,c) <- enumerate r] neighbors8 :: Point2 -> [Point2] neighbors8 (x,y) = [(x-1,y-1),(x,y-1),(x+1,y-1),(x-1,y),(x+1,y),(x-1,y+1),(x,y+1),(x+1,y+1)] neighbors4 :: Point2 -> [Point2] neighbors4 (x,y) = [(x-1,y), (x+1,y), (x,y-1), (x,y+1)] trim :: String -> String trim = f . f where f = reverse . dropWhile isSpace