diff --git a/src/Day3/Part1.idr b/src/Day3/Part1.idr index ca132be..3f26b3a 100644 --- a/src/Day3/Part1.idr +++ b/src/Day3/Part1.idr @@ -4,13 +4,10 @@ import Data.Either import Data.Maybe import Data.String import Data.String.Parser +import Utils --- PARSING -export -scan : Monad m => ParseT m a -> ParseT m a -scan p = many (requireFailure p *> skip (satisfy (const True))) *> p - export mul : Parser Integer mul = do diff --git a/src/Utils.idr b/src/Utils.idr index fa8e9b9..71811a4 100644 --- a/src/Utils.idr +++ b/src/Utils.idr @@ -2,9 +2,29 @@ module Utils import Data.Maybe import Data.String +import Data.String.Parser %default total export parseNat : String -> Maybe Nat parseNat = map cast . filter (> 0) . parseInteger + + +export +lookahead : Functor m => ParseT m a -> ParseT m a +lookahead p = P $ \s => map (\case + OK r _ => OK r s + Fail i err => Fail i err) + (p.runParser s) +export covering +until : Monad m => ParseT m a -> ParseT m () +until p = skip $ many (requireFailure p *> skip (satisfy (const True))) + +export covering +scan : Monad m => ParseT m a -> ParseT m a +scan p = until p *> p + +export covering +scanWhile : Monad m => ParseT m a -> ParseT m b -> ParseT m b +scanWhile w p = many (lookahead w *> requireFailure p *> skip (satisfy (const True))) *> p