X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-20.git;a=blobdiff_plain;f=advent02%2Fsrc%2Fadvent02.hs;fp=advent02%2Fsrc%2Fadvent02.hs;h=27886d05744f8c57507ef52f379f24b6e0b7f1fa;hp=0000000000000000000000000000000000000000;hb=72c69da4bb83a1a18965411c7a52b3f9b3a18d2d;hpb=d3f84f094ba00f34cd43eea17a7aab4200a8ef04 diff --git a/advent02/src/advent02.hs b/advent02/src/advent02.hs new file mode 100644 index 0000000..27886d0 --- /dev/null +++ b/advent02/src/advent02.hs @@ -0,0 +1,65 @@ +-- import Debug.Trace + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec hiding (State) +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +data Policy = Policy + { lower :: Int + , upper :: Int + , character :: Char + , password :: String + } deriving (Show, Eq, Ord) + +main :: IO () +main = + do text <- TIO.readFile "data/advent02.txt" + let policies = successfulParse text + print $ part1 policies + print $ part2 policies + -- print $ head $ part2 nums + +part1 = length . filter inRange + where nCharsPresent p = length $ filter (== (character p)) (password p) + inRange p = (nCharsPresent p >= (lower p)) && (nCharsPresent p <= (upper p)) + +part2 = length . filter matches + +matches p = ((pw!!l) == c) /= ((pw!!u) == c) + where pw = password p + c = character p + l = (lower p) - 1 + u = (upper p) - 1 + + +-- Parse the input file +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty +-- sc = L.space (skipSome (char ' ')) CA.empty CA.empty + +lexeme = L.lexeme sc +integerP = lexeme L.decimal +-- signedInteger = L.signed sc integer +symb = L.symbol sc +hyphenP = symb "-" +colonP = symb ":" +characterP = alphaNumChar <* sc +passwordP = some alphaNumChar <* sc + +policiesP = many policyP +policyP = Policy <$> integerP <* hyphenP <*> integerP <*> characterP <* colonP <*> passwordP +-- policyP = (,,,) <$> integerP <* hyphenP <*> integerP <*> characterP <* colonP <*> passwordP + +-- successfulParse :: Text -> [Policy] +successfulParse input = + case parse policiesP "input" input of + Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right policies -> policies