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

import Data.Char
import Control.Monad

countElem :: (Eq a) => a -> [a] -> Int
countElem c ls = sum $ map (fromEnum . (== c)) ls

xor :: Bool -> Bool -> Bool
xor True True   = False
xor True False  = True
xor False True  = True
xor False False = False

ranges :: [String] -> (Int, Int)
ranges r =
    let p = head r
        start = read $ takeWhile (/= '-') p
        end = abs . read $ dropWhile (/= '-') p
     in (start, end)

charac :: [String] -> Char
charac s = head (s !! 1)

pass :: [String] -> String
pass = last

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

doCheck2 :: ((Int, Int), Char , String) -> Bool
doCheck2 ((lower, upper), c, pass) = (pass !! (lower - 1) == c) `xor` (pass !! (upper - 1) == c)

main :: IO ()
main = do
    n <- map (fromEnum . doCheck2 . (\w -> (ranges w, charac w, pass w)) . words) . lines <$> readFile "input/02"
    print $ sum n