Skip to content

Commit

Permalink
nicer corner detection
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 12, 2024
1 parent bd551f9 commit 7d8c16e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions solutions/src/2024/12.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,16 @@ regions = unfoldr \input ->
step i = [j | j <- cardinal i, Map.lookup j input == Just label]
]


-- | Find the perimeter length of a region.
perimeter :: Set Coord -> Int
perimeter xs = length [() | x <- Set.toList xs, y <- cardinal x, y `Set.notMember` xs]

-- | Compute the number of walls needed to surround a region by looking for
-- the corners of the region.
walls :: Set Coord -> Int
walls xs =
let f i = Set.member i xs in
countBy (\x -> not (f (above x)) && (not (f (right x)) || f (above (right x)))) xs +
countBy (\x -> not (f (below x)) && (not (f (right x)) || f (below (right x)))) xs +
countBy (\x -> not (f (right x)) && (not (f (above x)) || f (above (right x)))) xs +
countBy (\x -> not (f (left x)) && (not (f (above x)) || f (above (left x)))) xs
walls xs
= countBy (corner left above) xs + countBy (corner above right) xs
+ countBy (corner below left ) xs + countBy (corner right below) xs
where
corner dir1 dir2 x = open dir1 && (open dir2 || not (open (dir1 . dir2)))
where open dir = dir x `Set.notMember` xs

0 comments on commit 7d8c16e

Please sign in to comment.