diff options
author | Akshay <[email protected]> | 2020-12-07 07:34:21 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-07 07:34:21 +0000 |
commit | 90ee9892e2e0fd188bac09c50987c967cf282333 (patch) | |
tree | 4d1793f1c216f31a526ad3d9316b7246fd835a51 | |
parent | 7c26160075af3ccd87e0029ecc0e09ad3ca2dc66 (diff) |
simplify day02
-rw-r--r-- | execs/Day02.hs | 27 |
1 files changed, 12 insertions, 15 deletions
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 | |||
4 | import Text.Parsec.Char | 4 | import Text.Parsec.Char |
5 | import Utils | 5 | import Utils |
6 | 6 | ||
7 | data PassProp = PassProp | 7 | type PassProp = (Int, Int, Char, String) |
8 | { range :: (Int, Int) | ||
9 | , c :: Char | ||
10 | , pass :: String | ||
11 | } deriving (Show) | ||
12 | 8 | ||
9 | parseInt :: Parser Int | ||
10 | parseInt = read <$> many1 digit | ||
13 | parseProp :: Parser PassProp | 11 | parseProp :: Parser PassProp |
14 | parseProp = do | 12 | parseProp = (,,,) |
15 | lower <- many1 digit | 13 | <$> parseInt <* char '-' |
16 | upper <- char '-' >> many1 digit | 14 | <*> parseInt <* space |
17 | c <- space >> letter | 15 | <*> letter <* string ": " |
18 | password <- string ": " >> many letter | 16 | <*> many letter |
19 | return $ PassProp (read lower, read upper) c password | ||
20 | 17 | ||
21 | doCheck1 :: PassProp -> Bool | 18 | doCheck1 :: PassProp -> Bool |
22 | doCheck1 PassProp { range = (lower, upper), c = c, pass = pass} = | 19 | doCheck1 (lower, upper, c, pass) = |
23 | count >= lower && count <= upper | 20 | count >= lower && count <= upper |
24 | where count = countElem c pass | 21 | where count = countElem c pass |
25 | 22 | ||
26 | doCheck2 :: PassProp -> Bool | 23 | doCheck2 :: PassProp -> Bool |
27 | doCheck2 PassProp { range = (lower, upper), c = c, pass = pass} = | 24 | doCheck2 (lower, upper, c, pass) = |
28 | (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c) | 25 | (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c) |
29 | 26 | ||
30 | parseLine :: Parser a -> String -> a | 27 | parseLine :: Parser a -> String -> a |
@@ -33,5 +30,5 @@ parseLine parser line = right $ parse parser "input" line | |||
33 | main :: IO () | 30 | main :: IO () |
34 | main = do | 31 | main = do |
35 | n <- map (parseLine parseProp) . lines <$> readFile "input/02" | 32 | n <- map (parseLine parseProp) . lines <$> readFile "input/02" |
36 | print $ countElem True (map doCheck1 n) | 33 | print $ howMany doCheck1 n |
37 | print $ countElem True (map doCheck2 n) | 34 | print $ howMany doCheck2 n |