X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-22.git;a=blobdiff_plain;f=advent02%2FMain.hs;fp=advent02%2FMain.hs;h=074157a0d9bdd8f6b50ed9695deed1875861f00a;hp=231531749fb68bb0ca9d11b7654e15eb3ae06c47;hb=402a27d38485107852e8128f20a443cc7a684c6d;hpb=d26baeb75e7bb105eb23c7434832203dc2ce6cfa diff --git a/advent02/Main.hs b/advent02/Main.hs index 2315317..074157a 100644 --- a/advent02/Main.hs +++ b/advent02/Main.hs @@ -1,7 +1,7 @@ -- Writeup at https://work.njae.me.uk/2022/12/02/advent-of-code-2022-day-2/ import AoC -import Data.Text () +import Data.Text (Text) import qualified Data.Text.IO as TIO import Data.Attoparsec.Text hiding (Result) import Control.Applicative @@ -52,6 +52,13 @@ roundFromResult (ShapeResult shape result) = Round shape p2s -- Parse the input file +match1P :: Parser [Round] +match2P :: Parser [ShapeResult] +roundP :: Parser Round +shapeResultP :: Parser ShapeResult +p1ShapeP, p2ShapeP, aP, bP, cP, xP, yP, zP :: Parser Shape +resultP, xrP, yrP, zrP :: Parser Result + match1P = roundP `sepBy` endOfLine roundP = Round <$> p1ShapeP <*> (" " *> p2ShapeP) @@ -73,13 +80,14 @@ xrP = Loss <$ "X" yrP = Draw <$ "Y" zrP = Win <$ "Z" --- successfulParse :: Text -> (Integer, [Maybe Integer]) +successfulParse1 :: Text -> [Round] successfulParse1 input = case parseOnly match1P input of Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err - Right match -> match + Right matches -> matches +successfulParse2 :: Text -> [ShapeResult] successfulParse2 input = case parseOnly match2P input of Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err - Right match -> match + Right matches -> matches