haskell: 2015 05b

This commit is contained in:
Maciej Jur 2024-08-06 23:35:39 +02:00
parent e2176c7893
commit b51254b67e
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
2 changed files with 20 additions and 7 deletions

View file

@ -20,5 +20,6 @@ main :: IO ()
main = do
content <- Day05.parse <$> readDay 5
print $ Day05.solveA content
print $ Day05.solveB content
-- print @Int $ Day05.solveB parsed

View file

@ -2,12 +2,12 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
module Day05 (parse, solveA) where
module Day05 (parse, solveA, solveB) where
import Control.Monad (foldM)
import Control.Monad qualified as T
import Data.Either (fromLeft)
import Data.Maybe (fromMaybe, isNothing)
import Data.List (tails)
import Data.Maybe (fromMaybe)
import Data.Text (Text)
import Data.Text qualified as T
@ -36,9 +36,21 @@ isNice = maybe False (check . snd) . foldM next ('_', (False, 0)) . T.unpack
solveA :: [Text] -> Int
solveA = length . filter isNice
test :: Text
test = "dvszwmarrgswjxmbau"
checkTriple :: Text -> Bool
checkTriple text
| T.length text > 2 = T.index text 0 == T.index text 2
| otherwise = False
-- $> isNice test
checkPairs :: Text -> Bool
checkPairs "" = False
checkPairs text =
let (cs, rest) = T.splitAt 2 text
in cs `T.isInfixOf` rest
solveB = undefined
isNiceB :: Text -> Bool
isNiceB text =
let parts = T.tails text
in any checkTriple parts && any checkPairs parts
solveB :: [Text] -> Int
solveB = length . filter isNiceB