diff options
| author | Laura Orvokki Kursula <lav@vampires.gay> | 2024-12-12 21:00:18 +0100 | 
|---|---|---|
| committer | Laura Orvokki Kursula <lav@vampires.gay> | 2024-12-12 21:00:18 +0100 | 
| commit | a49d636ee3595ade89fd10c7a869bdf2f2985774 (patch) | |
| tree | 4b2756dfa7b5065cb93f7b348ccc2dde9593097c | |
| parent | 447e9f5048ecee06afd9b545ccc445c232ec4635 (diff) | |
| download | aoc2024-a49d636ee3595ade89fd10c7a869bdf2f2985774.tar.gz aoc2024-a49d636ee3595ade89fd10c7a869bdf2f2985774.zip | |
12-1 hashset!
| -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' |