From 122b17f4ddbd114b735be92ba4e380d70f7cc867 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 12 Dec 2024 16:18:14 +0000 Subject: [PATCH] Tidying --- advent12/Main.hs | 9 +++++---- advent12/MainOriginal.hs | 21 --------------------- 2 files changed, 5 insertions(+), 25 deletions(-) diff --git a/advent12/Main.hs b/advent12/Main.hs index 242ca9b..bafb885 100644 --- a/advent12/Main.hs +++ b/advent12/Main.hs @@ -5,10 +5,9 @@ import Linear -- 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) @@ -39,11 +38,12 @@ class Ord a => Joinable a where | 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 @@ -63,7 +63,8 @@ class Ord a => Joinable a where 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 diff --git a/advent12/MainOriginal.hs b/advent12/MainOriginal.hs index f14ba1e..424d2f3 100644 --- a/advent12/MainOriginal.hs +++ b/advent12/MainOriginal.hs @@ -7,7 +7,6 @@ import qualified Data.Map.Strict as M 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) @@ -18,26 +17,6 @@ data SideFragment = SideFragment Position Facing 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 -- 2.34.1