aboutsummaryrefslogtreecommitdiff
path: root/execs/Day02.hs
blob: 412d1d1399c2697abf0c6cb80460bbbdab4ae90c (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.ParserCombinators.Parsec
import Text.Parsec.Char
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