-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{-# LANGUAGE ViewPatterns #-} | ||
|
||
-- | | ||
-- Module: Day12 | ||
-- Description: <https://adventofcode.com/2024/day/12 Day 12: Garden Groups> | ||
module Day12 (part1, part2) where | ||
|
||
import Control.Arrow (first) | ||
import Control.Monad (ap) | ||
import Data.Either (partitionEithers) | ||
import Data.Function (on) | ||
import Data.List (groupBy) | ||
import Data.Map qualified as Map (delete, filter, fromDistinctAscList, keys, minViewWithKey, size, (!?)) | ||
import Data.Map.Strict qualified as Map (fromListWith) | ||
import Data.Semigroup (Arg (Arg)) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (lines, unpack) | ||
|
||
solve :: ([(Int, Int)] -> Int) -> Text -> Int | ||
solve perimeter input = | ||
go 0 . Map.fromDistinctAscList $ | ||
[ ((y, x), c) | ||
| (y, line) <- zip [0 ..] $ T.lines input, | ||
(x, c) <- zip [0 ..] $ T.unpack line | ||
] | ||
where | ||
go !cost plots@(Map.minViewWithKey -> Just ((pos, c), _)) = | ||
let (points, plots') = dfs pos c plots | ||
in go (cost + length points * perimeter points) plots' | ||
go cost _ = cost | ||
dfs pos@(y, x) c plots | ||
| plots Map.!? pos /= Just c = ([], plots) | ||
| otherwise = (pos : points1 ++ points2 ++ points3 ++ points4, plots4) | ||
where | ||
plots0 = Map.delete pos plots | ||
(points1, plots1) = dfs (y - 1, x) c plots0 | ||
(points2, plots2) = dfs (y, x - 1) c plots1 | ||
(points3, plots3) = dfs (y, x + 1) c plots2 | ||
(points4, plots4) = dfs (y + 1, x) c plots3 | ||
|
||
part1 :: Text -> Int | ||
part1 = solve $ \points -> | ||
Map.size . Map.filter (== 1) . Map.fromListWith (+) . map (,1 :: Int) $ do | ||
(y, x) <- points | ||
[Left (y, x), Right (x, y), Right (x + 1, y), Left (y + 1, x)] | ||
|
||
part2 :: Text -> Int | ||
part2 = solve $ \points -> | ||
let getArg (Arg (Left a) b) = Left (a, b) | ||
getArg (Arg (Right a) b) = Right (a, b) | ||
(horizontalEdges, verticalEdges) = | ||
partitionEithers . map getArg . Map.keys . Map.filter (== 1) . Map.fromListWith (+) $ do | ||
(y, x) <- points | ||
(,1 :: Int) | ||
<$> [ Arg (Left (y, x)) True, | ||
Arg (Right (x, y)) False, | ||
Arg (Right (x + 1, y)) True, | ||
Arg (Left (y + 1, x)) False | ||
] | ||
countConsecutive = succ . length . filter not . (zipWith ok `ap` drop 1) | ||
ok (a, b) (c, d) = abs (c - a) <= 1 && b == d | ||
in sum | ||
[ countConsecutive $ first snd <$> edges | ||
| edges <- | ||
groupBy ((==) `on` fst . fst) horizontalEdges | ||
++ groupBy ((==) `on` fst . fst) verticalEdges | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day12Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day12 (part1, part2) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2, example3, example4, example5 :: Text | ||
example1 = | ||
T.unlines | ||
[ "AAAA", | ||
"BBCD", | ||
"BBCC", | ||
"EEEC" | ||
] | ||
example2 = | ||
T.unlines | ||
[ "OOOOO", | ||
"OXOXO", | ||
"OOOOO", | ||
"OXOXO", | ||
"OOOOO" | ||
] | ||
example3 = | ||
T.unlines | ||
[ "RRRRIICCFF", | ||
"RRRRIICCCF", | ||
"VVRRRCCFFF", | ||
"VVRCCCJFFF", | ||
"VVVVCJJCFE", | ||
"VVIVCCJJEE", | ||
"VVIIICJJEE", | ||
"MIIIIIJJEE", | ||
"MIIISIJEEE", | ||
"MMMISSJEEE" | ||
] | ||
example4 = | ||
T.unlines | ||
[ "EEEEE", | ||
"EXXXX", | ||
"EEEEE", | ||
"EXXXX", | ||
"EEEEE" | ||
] | ||
example5 = | ||
T.unlines | ||
[ "AAAAAA", | ||
"AAABBA", | ||
"AAABBA", | ||
"ABBAAA", | ||
"ABBAAA", | ||
"AAAAAA" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
part1 example1 `shouldBe` 140 | ||
part1 example2 `shouldBe` 772 | ||
part1 example3 `shouldBe` 1930 | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example1 `shouldBe` 80 | ||
part2 example2 `shouldBe` 436 | ||
part2 example4 `shouldBe` 236 | ||
part2 example5 `shouldBe` 368 | ||
part2 example3 `shouldBe` 1206 |