aboutsummaryrefslogtreecommitdiff
path: root/execs/Day02.hs
blob: c462ec926787ca9eccb9dd6d6ee87ec352c90d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Main where

import Text.ParserCombinators.Parsec
import Text.Parsec.Char
import Utils

data PassProp = PassProp
    { range :: (Int, Int)
    , c :: Char
    , pass :: String
    } deriving (Show)

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

doCheck1 :: PassProp -> Bool
doCheck1 PassProp { range = (lower, upper), c = c, pass = pass} =
    count >= lower && count <= upper
        where count = countElem c pass

doCheck2 :: PassProp -> Bool
doCheck2 PassProp { range = (lower, upper), c = c, pass = pass} =
    (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c)

parseLine :: Parser a -> String -> a
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)