-
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.
Merge pull request #124 from ephemient/hs/day19
Day 19: Linen Layout
- Loading branch information
Showing
6 changed files
with
75 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,34 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day19 | ||
-- Description: <https://adventofcode.com/2024/day/19 Day 19: Linen Layout> | ||
module Day19 (solve) where | ||
|
||
import Control.Arrow ((***)) | ||
import Control.Monad.ST (runST) | ||
import Data.Foldable (foldMap') | ||
import Data.List (sort) | ||
import Data.Monoid (Sum (Sum, getSum)) | ||
import Data.Text (Text) | ||
import Data.Text qualified as T (isPrefixOf, length, lines, null, splitOn, tails) | ||
import Data.Vector.Unboxed.Mutable qualified as MV (new, read, write) | ||
|
||
count :: [Text] -> Text -> (Sum Int, Sum Int) | ||
count keys target = runST $ do | ||
acc <- MV.new $ T.length target + 1 | ||
MV.write acc 0 1 | ||
mapM_ (\(i, j) -> (+) <$> MV.read acc i <*> MV.read acc j >>= MV.write acc i) . sort $ | ||
[ (offset + T.length key, offset) | ||
| key <- keys, | ||
(offset, target') <- zip [0 ..] $ T.tails target, | ||
key `T.isPrefixOf` target' | ||
] | ||
n <- MV.read acc $ T.length target | ||
pure (Sum $ if n == 0 then 0 else 1, Sum n) | ||
|
||
solve :: Text -> (Int, Int) | ||
solve input | ||
| keys : rest <- T.lines input = | ||
(getSum *** getSum) $ foldMap' (count $ T.splitOn ", " keys) $ filter (not . T.null) rest | ||
| otherwise = (0, 0) |
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,29 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day19Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day19 (solve) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example :: Text | ||
example = | ||
T.unlines | ||
[ "r, wr, b, g, bwu, rb, gb, br", | ||
"", | ||
"brwrr", | ||
"bggr", | ||
"gbbr", | ||
"rrbgbr", | ||
"ubwu", | ||
"bwurrg", | ||
"brgr", | ||
"bbrgwb" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "solve" $ do | ||
it "examples" $ do | ||
solve example `shouldBe` (6, 16) |