diff options
-rw-r--r-- | 12-1.hs | 11 |
1 files changed, 6 insertions, 5 deletions
@@ -1,4 +1,5 @@ import Data.Array +import Data.HashSet as HS (HashSet, fromList, member, singleton, toList) import Data.List (nub) type Map = Array (Int,Int) (Maybe Char) @@ -25,18 +26,18 @@ findNext = go | otherwise = Nothing getRegion :: Map -> (Int,Int) -> [(Int,Int)] -getRegion m p = go [p] [p] +getRegion m p = go (HS.singleton p) [p] where - go :: [(Int,Int)] -> [(Int,Int)] -> [(Int,Int)] + go :: HS.HashSet (Int,Int) -> [(Int,Int)] -> [(Int,Int)] go visited points = let v = filter f - . filter (not . (`elem` visited)) + . filter (not . (`HS.member` visited)) . nub . concatMap neighbours $ points in case v of - [] -> visited - _ -> go (visited ++ v) v + [] -> HS.toList visited + _ -> go (visited <> HS.fromList v) v f :: (Int,Int) -> Bool f p' = inRange (bounds m) p' && m ! p == m ! p' |