Day2: Make pretty

This commit is contained in:
shu 2020-12-02 07:46:44 +01:00
parent 3a4709d5b2
commit ea7ef85958

@ -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