diff --git a/2020/day02/day02.hs b/2020/day02/day02.hs index 7251bd5..f042fea 100644 --- a/2020/day02/day02.hs +++ b/2020/day02/day02.hs @@ -1,25 +1,25 @@ -import Control.Arrow -import Control.Monad import Data.List.Split main :: IO () main = do input <- lines <$> readFile "input" - print $ day2a input - print $ day2b input + print $ day2 f1 input + print $ day2 f2 input -predicate :: String -> Bool -predicate line = (l>=a) && (l<=b) - where c = head $ words p !! 1 - [a,b] = map read $ splitOn "-" $ head $ words p - l = length $ filter (==c) str - [p,str] = splitOn ": " line +day2 :: (String -> Int -> Int -> Char -> Bool) -> [String] -> Int +day2 f = length . filter (predicate f) -day2a = length . filter predicate -day2b = length . filter predicate2 +f1 :: String -> Int -> Int -> Char -> Bool +f1 str a b c = (l >= a) && (l <= b) + where + l = length $ filter (== c) str -predicate2 :: String -> Bool -predicate2 line = (str!!a==c) /= (str!!b==c) - where c = head $ words p !! 1 - [a,b] = map (subtract 1 . read) $ splitOn "-" $ head $ words p - [p,str] = splitOn ": " line +f2 :: String -> Int -> Int -> Char -> Bool +f2 str a b c = (str !! pred a == c) /= (str !! pred b == c) + +predicate :: (String -> Int -> Int -> Char -> Bool) -> String -> Bool +predicate f line = f str a b c + where + c = head $ words p !! 1 + [a, b] = map read $ splitOn "-" $ head $ words p + [p, str] = splitOn ": " line