AoC/2020/day02/day02.hs

26 lines
691 B
Haskell
Raw Normal View History

2020-12-02 06:51:03 +01:00
import Data.List.Split
main :: IO ()
main = do
input <- lines <$> readFile "input"
2020-12-02 07:46:44 +01:00
print $ day2 f1 input
print $ day2 f2 input
2020-12-02 06:51:03 +01:00
2020-12-02 07:46:44 +01:00
day2 :: (String -> Int -> Int -> Char -> Bool) -> [String] -> Int
day2 f = length . filter (predicate f)
2020-12-02 06:51:03 +01:00
2020-12-02 07:46:44 +01:00
f1 :: String -> Int -> Int -> Char -> Bool
f1 str a b c = (l >= a) && (l <= b)
where
l = length $ filter (== c) str
2020-12-02 06:51:03 +01:00
2020-12-02 07:46:44 +01:00
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