From 7e617b670988e50c43a140a19033162c8695b716 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 2 Dec 2020 11:10:11 +0530 Subject: add initial day 2 solution --- execs/Day02.hs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 execs/Day02.hs (limited to 'execs') diff --git a/execs/Day02.hs b/execs/Day02.hs new file mode 100644 index 0000000..bcb0896 --- /dev/null +++ b/execs/Day02.hs @@ -0,0 +1,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 + -- cgit v1.2.3