This repository was archived by the owner on Nov 17, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ module AOC.Common.Point (
39
39
, boundingBox'
40
40
, inBoundingBox
41
41
, minCorner , minCorner'
42
+ , contiguousRegions
42
43
, shiftToZero
43
44
, shiftToZero'
44
45
, parseAsciiMap
@@ -171,6 +172,26 @@ fullNeighbsSet p = S.fromDistinctAscList $
171
172
, d /= pure 0
172
173
]
173
174
175
+ -- | Find contiguous regions by cardinal neighbors
176
+ contiguousRegions
177
+ :: Set Point
178
+ -> Set (Set Point )
179
+ contiguousRegions = startNewPool S. empty
180
+ where
181
+ startNewPool seenPools remaining = case S. minView remaining of
182
+ Nothing -> seenPools
183
+ Just (x, xs) ->
184
+ let (newPool, remaining') = fillUp (NES. singleton x) S. empty xs
185
+ in startNewPool (S. insert newPool seenPools) remaining'
186
+ fillUp boundary internal remaining = case NES. nonEmptySet newBoundary of
187
+ Nothing -> (newInternal, remaining)
188
+ Just nb -> fillUp nb newInternal newRemaining
189
+ where
190
+ edgeCandidates = foldMap' cardinalNeighbsSet boundary `S.difference` internal
191
+ newBoundary = edgeCandidates `S.intersection` remaining
192
+ newInternal = internal `S.union` NES. toSet boundary
193
+ newRemaining = remaining `S.difference` edgeCandidates
194
+
174
195
memoPoint :: Memo Point
175
196
memoPoint = Memo. wrap (uncurry V2 ) (\ (V2 x y) -> (x, y)) $
176
197
Memo. pair Memo. integral Memo. integral
You can’t perform that action at this time.
0 commit comments