aboutsummaryrefslogtreecommitdiff
path: root/execs/Day02.hs
blob: 43f6e31d9828c134f9fd5970f4462c9ff0567fea (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
module Main where

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

type PassProp = (Int, Int, Char, String)

parseInt :: Parser Int
parseInt = read <$> many1 digit
parseProp :: Parser PassProp
parseProp = (,,,)
            <$> parseInt <* char '-'
            <*> parseInt <* space
            <*> letter <* string ": "
            <*> many letter

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

doCheck2 :: PassProp -> Bool
doCheck2 (lower, upper, c, 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 $ howMany doCheck1 n
    print $ howMany doCheck2 n