summaryrefslogtreecommitdiff
path: root/12-1.hs
diff options
context:
space:
mode:
authorLaura Orvokki Kursula <lav@vampires.gay>2024-12-12 21:00:18 +0100
committerLaura Orvokki Kursula <lav@vampires.gay>2024-12-12 21:00:18 +0100
commita49d636ee3595ade89fd10c7a869bdf2f2985774 (patch)
tree4b2756dfa7b5065cb93f7b348ccc2dde9593097c /12-1.hs
parent447e9f5048ecee06afd9b545ccc445c232ec4635 (diff)
downloadaoc2024-a49d636ee3595ade89fd10c7a869bdf2f2985774.tar.gz
aoc2024-a49d636ee3595ade89fd10c7a869bdf2f2985774.zip
12-1 hashset!
Diffstat (limited to '12-1.hs')
-rw-r--r--12-1.hs11
1 files changed, 6 insertions, 5 deletions
diff --git a/12-1.hs b/12-1.hs
index 78ea186..11b793f 100644
--- a/12-1.hs
+++ b/12-1.hs
@@ -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'