aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
Diffstat (limited to 'execs')
-rw-r--r--execs/Day02.hs27
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
4import Text.Parsec.Char 4import Text.Parsec.Char
5import Utils 5import Utils
6 6
7data PassProp = PassProp 7type PassProp = (Int, Int, Char, String)
8 { range :: (Int, Int)
9 , c :: Char
10 , pass :: String
11 } deriving (Show)
12 8
9parseInt :: Parser Int
10parseInt = read <$> many1 digit
13parseProp :: Parser PassProp 11parseProp :: Parser PassProp
14parseProp = do 12parseProp = (,,,)
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
21doCheck1 :: PassProp -> Bool 18doCheck1 :: PassProp -> Bool
22doCheck1 PassProp { range = (lower, upper), c = c, pass = pass} = 19doCheck1 (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
26doCheck2 :: PassProp -> Bool 23doCheck2 :: PassProp -> Bool
27doCheck2 PassProp { range = (lower, upper), c = c, pass = pass} = 24doCheck2 (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
30parseLine :: Parser a -> String -> a 27parseLine :: Parser a -> String -> a
@@ -33,5 +30,5 @@ parseLine parser line = right $ parse parser "input" line
33main :: IO () 30main :: IO ()
34main = do 31main = 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