aboutsummaryrefslogtreecommitdiff
path: root/2024/12-Garden_Groups
diff options
context:
space:
mode:
Diffstat (limited to '2024/12-Garden_Groups')
-rw-r--r--2024/12-Garden_Groups/example14
-rw-r--r--2024/12-Garden_Groups/example25
-rw-r--r--2024/12-Garden_Groups/example310
-rw-r--r--2024/12-Garden_Groups/example45
-rw-r--r--2024/12-Garden_Groups/example56
-rw-r--r--2024/12-Garden_Groups/first.hs80
-rw-r--r--2024/12-Garden_Groups/input140
-rw-r--r--2024/12-Garden_Groups/second.hs99
8 files changed, 349 insertions, 0 deletions
diff --git a/2024/12-Garden_Groups/example1 b/2024/12-Garden_Groups/example1
new file mode 100644
index 0000000..b41163a
--- /dev/null
+++ b/2024/12-Garden_Groups/example1
@@ -0,0 +1,4 @@
+AAAA
+BBCD
+BBCC
+EEEC
diff --git a/2024/12-Garden_Groups/example2 b/2024/12-Garden_Groups/example2
new file mode 100644
index 0000000..50a7304
--- /dev/null
+++ b/2024/12-Garden_Groups/example2
@@ -0,0 +1,5 @@
+OOOOO
+OXOXO
+OOOOO
+OXOXO
+OOOOO
diff --git a/2024/12-Garden_Groups/example3 b/2024/12-Garden_Groups/example3
new file mode 100644
index 0000000..85b768f
--- /dev/null
+++ b/2024/12-Garden_Groups/example3
@@ -0,0 +1,10 @@
+RRRRIICCFF
+RRRRIICCCF
+VVRRRCCFFF
+VVRCCCJFFF
+VVVVCJJCFE
+VVIVCCJJEE
+VVIIICJJEE
+MIIIIIJJEE
+MIIISIJEEE
+MMMISSJEEE
diff --git a/2024/12-Garden_Groups/example4 b/2024/12-Garden_Groups/example4
new file mode 100644
index 0000000..5818d30
--- /dev/null
+++ b/2024/12-Garden_Groups/example4
@@ -0,0 +1,5 @@
+EEEEE
+EXXXX
+EEEEE
+EXXXX
+EEEEE
diff --git a/2024/12-Garden_Groups/example5 b/2024/12-Garden_Groups/example5
new file mode 100644
index 0000000..f7cd38f
--- /dev/null
+++ b/2024/12-Garden_Groups/example5
@@ -0,0 +1,6 @@
+AAAAAA
+AAABBA
+AAABBA
+ABBAAA
+ABBAAA
+AAAAAA
diff --git a/2024/12-Garden_Groups/first.hs b/2024/12-Garden_Groups/first.hs
new file mode 100644
index 0000000..d163999
--- /dev/null
+++ b/2024/12-Garden_Groups/first.hs
@@ -0,0 +1,80 @@
+-- requires cabal install --lib megaparsec parser-combinators heap vector
+module Main (main) where
+
+import Control.Monad (void, when)
+import qualified Data.List as L
+import qualified Data.Map as M
+import qualified Data.Vector as V
+import qualified Data.Vector.Unboxed as VU
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+
+example1ExpectedOutput = 140
+example2ExpectedOutput = 772
+example3ExpectedOutput = 1930
+
+type Line = VU.Vector Char
+type Input = V.Vector Line
+
+type Parser = Parsec Void String
+
+parseLine :: Parser Line
+parseLine = do
+ line <- some letterChar <* eol
+ return $ VU.generate (length line) (line !!)
+
+parseInput' :: Parser Input
+parseInput' = do
+ line <- some parseLine <* eof
+ return $ V.generate (length line) (line !!)
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> error $ errorBundlePretty bundle
+ Right input' -> return input'
+
+type Coord = (Int, Int)
+type Visited = M.Map Coord ()
+
+compute :: Input -> Int
+compute input = fst $ V.ifoldl' compute' (0, M.empty) input
+ where
+ compute' :: (Int, Visited) -> Int -> Line -> (Int, Visited)
+ compute' (acc, visited) y line = VU.ifoldl' (compute'' y) (acc, visited) line
+ compute'' :: Int -> (Int, Visited) -> Int -> Char -> (Int, Visited)
+ compute'' y acc@(cost, visited) x c = case M.lookup (x, y) visited of
+ Just _ -> acc -- already processed
+ Nothing -> let (area, perimeter, visited') = enclose c (0, 0, visited) (x, y) in (cost + area * perimeter, M.union visited visited')
+ enclose :: Char -> (Int, Int, Visited) -> Coord -> (Int, Int, Visited)
+ enclose c acc@(area, perimeter, visited) (x, y) = case M.lookup (x, y) visited of
+ Just _ -> acc
+ Nothing -> case input V.!? y of
+ Just line -> case line VU.!? x of
+ Just c' -> let n = neighbors visited c x y in L.foldl' (enclose c) (1 + area, 4 - length n + perimeter, M.insert (x, y) () visited) n
+ Nothing -> (0, 0, visited)
+ Nothing -> (0, 0, visited)
+ neighbors :: Visited -> Char -> Int -> Int -> [Coord]
+ neighbors visited c x y = filter (valid visited c) [ (x-1, y), (x+1, y), (x, y-1), (x, y+1) ]
+ valid :: Visited -> Char -> Coord -> Bool
+ valid visited c (x, y) = case input V.!? y of
+ Just line -> case line VU.!? x of
+ Just c' -> c == c'
+ Nothing -> False
+ Nothing -> False
+
+main :: IO ()
+main = do
+ example1 <- parseInput "example1"
+ let exampleOutput = compute example1
+ when (exampleOutput /= example1ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example1ExpectedOutput)
+ example2 <- parseInput "example2"
+ let exampleOutput = compute example2
+ when (exampleOutput /= example2ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example2ExpectedOutput)
+ example3 <- parseInput "example3"
+ let exampleOutput = compute example3
+ when (exampleOutput /= example3ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example3ExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input
diff --git a/2024/12-Garden_Groups/input b/2024/12-Garden_Groups/input
new file mode 100644
index 0000000..aad0948
--- /dev/null
+++ b/2024/12-Garden_Groups/input
@@ -0,0 +1,140 @@
+QQQQQQQQQQQQQQQQQQQWWWWWWWWRYYYYYYYYYYOOOOMOYYYYBBYYYYBBBGGGGGVVVVVNNNNPPOOOOOOOOOOOOOGGGGGGGGGGKTTTTTTTVVVVKKKKKKKKKKKKKKKKPDPBBBBBBBBBBBBB
+QQAQAQQQQQQQQQQQQMMWWWWWWWWRRRYRYOOYOOOOOOOOHHHYBBBYBBBBBGGGVGVVVVVNPPPPPPOOOOOOOOOOOGGQGGGGGGGGGTTTTTTTVVKKKKKKKKKKKKKKKKKKPPPPPBBBBBBBBBBB
+QQAAAQQQ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second.hs
new file mode 100644
index 0000000..98409d6
--- /dev/null
+++ b/2024/12-Garden_Groups/second.hs
@@ -0,0 +1,99 @@
+-- requires cabal install --lib megaparsec parser-combinators heap vector
+module Main (main) where
+
+import Control.Monad (void, when)
+import qualified Data.List as L
+import qualified Data.Map as M
+import qualified Data.Vector as V
+import qualified Data.Vector.Unboxed as VU
+import Data.Void (Void)
+import Text.Megaparsec
+import Text.Megaparsec.Char
+
+example1ExpectedOutput = 80
+example2ExpectedOutput = 436
+example3ExpectedOutput = 1206
+example4ExpectedOutput = 236
+example5ExpectedOutput = 368
+
+type Line = VU.Vector Char
+type Input = V.Vector Line
+
+type Parser = Parsec Void String
+
+parseLine :: Parser Line
+parseLine = do
+ line <- some letterChar <* eol
+ return $ VU.generate (length line) (line !!)
+
+parseInput' :: Parser Input
+parseInput' = do
+ line <- some parseLine <* eof
+ return $ V.generate (length line) (line !!)
+
+parseInput :: String -> IO Input
+parseInput filename = do
+ input <- readFile filename
+ case runParser parseInput' filename input of
+ Left bundle -> error $ errorBundlePretty bundle
+ Right input' -> return input'
+
+type Coord = (Int, Int)
+type Visited = M.Map Coord ()
+type Region = [Coord]
+
+compute :: Input -> Int
+compute input = sum $ map (\r -> corners r * length r) regions
+ where
+ (regions, _) = V.ifoldl' regions' ([], M.empty) input
+ regions' :: ([Region], Visited) -> Int -> Line -> ([Region], Visited)
+ regions' acc y line = VU.ifoldl' (regions'' y) acc line
+ regions'' :: Int -> ([Region], Visited) -> Int -> Char -> ([Region], Visited)
+ regions'' y acc@(rs, visited) x c = case M.lookup (x, y) visited of
+ Just _ -> acc -- already processed
+ Nothing -> let (r, visited') = flood c ([], visited) (x, y) in (r:rs, visited')
+ flood :: Char -> (Region, Visited) -> (Int, Int) -> (Region, Visited)
+ flood c acc@(rs, visited) (x, y) = case M.lookup (x, y) visited of
+ Just _ -> acc
+ Nothing -> L.foldl' (flood c) ((x, y):rs, M.insert (x, y) () visited) $ neighbors visited c x y
+ neighbors :: Visited -> Char -> Int -> Int -> [Coord]
+ neighbors visited c x y = filter (valid visited c) [ (x-1, y), (x+1, y), (x, y-1), (x, y+1) ]
+ valid :: Visited -> Char -> (Int, Int) -> Bool
+ valid visited c (x, y) = case input V.!? y of
+ Just line -> case line VU.!? x of
+ Just c' -> c == c'
+ Nothing -> False
+ Nothing -> False
+ corners :: Region -> Int
+ corners r = sum $ map corners' r
+ where
+ inside c = L.elem c r
+ outside = not . inside
+ corners' (x, y) = length $ filter id [ outside (x-1, y) && outside (x, y-1) -- top left in
+ , outside (x+1, y) && outside (x, y-1) -- top right in
+ , outside (x-1, y) && outside (x, y+1) -- bottom left in
+ , outside (x+1, y) && outside (x, y+1) -- bottom right in
+ , inside (x-1, y) && outside (x-1, y-1) && inside (x, y-1) -- top left out
+ , inside (x+1, y) && outside (x+1, y-1) && inside (x, y-1) -- top right out
+ , inside (x-1, y) && outside (x-1, y+1) && inside (x, y+1) -- bottom left out
+ , inside (x+1, y) && outside (x+1, y+1) && inside (x, y+1) ] -- bottom right out
+
+main :: IO ()
+main = do
+ example1 <- parseInput "example1"
+ let exampleOutput = compute example1
+ when (exampleOutput /= example1ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example1ExpectedOutput)
+ example2 <- parseInput "example2"
+ let exampleOutput = compute example2
+ when (exampleOutput /= example2ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example2ExpectedOutput)
+ example4 <- parseInput "example4"
+ let exampleOutput = compute example4
+ when (exampleOutput /= example4ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example4ExpectedOutput)
+ example5 <- parseInput "example5"
+ let exampleOutput = compute example5
+ when (exampleOutput /= example5ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example5ExpectedOutput)
+ example3 <- parseInput "example3"
+ let exampleOutput = compute example3
+ when (exampleOutput /= example3ExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show example3ExpectedOutput)
+ input <- parseInput "input"
+ print $ compute input