From 90ee9892e2e0fd188bac09c50987c967cf282333 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 7 Dec 2020 13:04:21 +0530 Subject: simplify day02 --- execs/Day02.hs | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'execs') diff --git a/execs/Day02.hs b/execs/Day02.hs index c462ec9..412d1d1 100644 --- a/execs/Day02.hs +++ b/execs/Day02.hs @@ -4,27 +4,24 @@ import Text.ParserCombinators.Parsec import Text.Parsec.Char import Utils -data PassProp = PassProp - { range :: (Int, Int) - , c :: Char - , pass :: String - } deriving (Show) +type PassProp = (Int, Int, Char, String) +parseInt :: Parser Int +parseInt = read <$> many1 digit parseProp :: Parser PassProp -parseProp = do - lower <- many1 digit - upper <- char '-' >> many1 digit - c <- space >> letter - password <- string ": " >> many letter - return $ PassProp (read lower, read upper) c password +parseProp = (,,,) + <$> parseInt <* char '-' + <*> parseInt <* space + <*> letter <* string ": " + <*> many letter doCheck1 :: PassProp -> Bool -doCheck1 PassProp { range = (lower, upper), c = c, pass = pass} = +doCheck1 (lower, upper, c, pass) = count >= lower && count <= upper where count = countElem c pass doCheck2 :: PassProp -> Bool -doCheck2 PassProp { range = (lower, upper), c = c, pass = pass} = +doCheck2 (lower, upper, c, pass) = (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c) parseLine :: Parser a -> String -> a @@ -33,5 +30,5 @@ parseLine parser line = right $ parse parser "input" line main :: IO () main = do n <- map (parseLine parseProp) . lines <$> readFile "input/02" - print $ countElem True (map doCheck1 n) - print $ countElem True (map doCheck2 n) + print $ howMany doCheck1 n + print $ howMany doCheck2 n -- cgit v1.2.3