AoC/2020/day02/day02.hs
2020-12-02 07:46:44 +01:00

26 lines
691 B
Haskell

import Data.List.Split
main :: IO ()
main = do
input <- lines <$> readFile "input"
print $ day2 f1 input
print $ day2 f2 input
day2 :: (String -> Int -> Int -> Char -> Bool) -> [String] -> Int
day2 f = length . filter (predicate f)
f1 :: String -> Int -> Int -> Char -> Bool
f1 str a b c = (l >= a) && (l <= b)
where
l = length $ filter (== c) str
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