-- import qualified Data.Set as S
import qualified Data.Map.Strict as M
import Data.Map.Strict ((!))
-import Data.List (foldl')
+import Data.List (foldl', nub)
type Position = V2 Int -- r, c
--- type Bounds = (Position, Position)
data Plot = Plot { pos :: Position, plant :: Char, fenceLength :: Int }
deriving (Show, Eq, Ord)
| x' == y' = uf
| rankX < rankY = M.insert x' (UFElement y' rankX) uf
| rankX > rankY = M.insert y' (UFElement x' rankY) uf
- | otherwise = M.insert y' (UFElement x' (rankX + 1)) uf
+ | otherwise = M.insert y' (UFElement x' rankY) $ M.insert x' newRoot uf
where x' = exemplar uf x
y' = exemplar uf y
UFElement _ rankX = uf ! x'
UFElement _ rankY = uf ! y'
+ newRoot = UFElement x' (rankX + 1)
merge :: UFind a -> UFind a
merge uf = foldl' mergeItem uf $ M.keys uf
meets :: a -> a -> Bool
instance Joinable Plot where
- meets plot1 plot2 = plot1.pos `elem` neighbours plot2.pos && plot1.plant == plot2.plant
+ meets plot1 plot2 =
+ plot1.pos `elem` neighbours plot2.pos && plot1.plant == plot2.plant
instance Joinable SideFragment where
meets (SideFragment p1 T) (SideFragment p2 T) = p1 `elem` neighboursH p2
import Data.Map.Strict ((!))
type Position = V2 Int -- r, c
--- type Bounds = (Position, Position)
data Plot = Plot { pos :: Position, plant :: Char, fenceLength :: Int }
deriving (Show, Eq, Ord)
deriving (Show, Eq, Ord)
type Side = S.Set SideFragment
-data UFElement a = UFElemeent a Int -- the rank
- deriving (Show, Eq, Ord)
-
-type UFind a = M.Map a (UFElement a)
-
-class Joinable a where
- exemplar :: UFind a -> a -> a
- join :: UFind a -> a -> a -> UFind a
- meets :: a -> a -> Bool
-
-instance Joinable Plot where
- meets plot1 plot2 = plot1.pos `elem` neighbours plot2.pos && plot1.plant == plot2.plant
-
-instance Joinable SideFragment where
- meets (SideFragment p1 T) (SideFragment p2 T) = p1 `elem` neighboursH p2
- meets (SideFragment p1 B) (SideFragment p2 B) = p1 `elem` neighboursH p2
- meets (SideFragment p1 L) (SideFragment p2 L) = p1 `elem` neighboursV p2
- meets (SideFragment p1 R) (SideFragment p2 R) = p1 `elem` neighboursV p2
- meets _ _ = False
-
main :: IO ()
main =
do dataFileName <- getDataFileName