Skip to content

Commit

Permalink
Merge pull request #228 from ephemient/hs/day19
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Jan 7, 2025
2 parents 613ba9a + b4c3517 commit 37f3a6a
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions hs/src/Day19.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,20 @@
-- Description: <https://adventofcode.com/2024/day/19 Day 19: Linen Layout>
module Day19 (solve) where

import Control.Parallel.Strategies
import Control.Parallel.Strategies (parMap, rseq)
import Data.Foldable (foldMap')
import Data.Monoid (Sum (Sum))
import Data.Text (Text)
import Data.Text qualified as T (lines, null, splitOn)
import Data.Text.Array qualified as A (equal)
import Data.Text.Internal (Text (Text))
import Data.Text.Unsafe qualified as T (lengthWord8)
import Data.Text qualified as T (isSuffixOf, lines, null, splitOn)
import Data.Text.Unsafe qualified as T (lengthWord8, takeWord8)
import Data.Vector qualified as V (generate, (!))

count :: [Text] -> Text -> Int
count keys target = getCount $ T.lengthWord8 target
where
counts = V.generate (T.lengthWord8 target) getCount
getCount 0 = 1
getCount i = sum [counts V.! (i - T.lengthWord8 key) | key <- keys, key `isSuffixOfAt` i $ target]

isSuffixOfAt :: Text -> Int -> Text -> Bool
isSuffixOfAt (Text a aoff alen) n (Text b boff blen) =
alen <= n && n <= blen && A.equal a aoff b (boff + n - alen) alen
getCount i = sum [counts V.! (i - T.lengthWord8 key) | key <- keys, key `T.isSuffixOf` T.takeWord8 i target]

solve :: Text -> Maybe (Int, Int)
solve (T.lines -> (T.splitOn ", " -> keys) : rest) | not $ any T.null keys = Just (part1, part2)
Expand Down

0 comments on commit 37f3a6a

Please sign in to comment.