diff --git a/common/AoC.hs b/common/AoC.hs index bdc48cf..cd4fa64 100644 --- a/common/AoC.hs +++ b/common/AoC.hs @@ -1,5 +1,7 @@ module AoC where +import Data.Function (on) +import Data.List (groupBy, intercalate) import Text.ParserCombinators.ReadP (.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d @@ -11,6 +13,9 @@ oneCompleteResult p s = case readP_to_S (p <* eof) s of [(x, "")] -> Just x _ -> Nothing +splitOnEmptyLines :: String -> [String] +splitOnEmptyLines = map (intercalate " ") . filter (not . any null) . groupBy ((==) `on` null) . lines + runAoC :: (Show r1, Show r2) => (String -> i) -> (i -> r1) -> (i -> r2) -> IO () runAoC inputTransform part1 part2 = do contents <- inputTransform <$> getContents diff --git a/day4/day4.hs b/day4/day4.hs index 36b935f..fd643af 100644 --- a/day4/day4.hs +++ b/day4/day4.hs @@ -3,8 +3,6 @@ import AoC import Control.Applicative ((<|>)) import Control.Monad (guard, mfilter) import Data.Char (isDigit) -import Data.Function (on) -import Data.List (groupBy, intercalate) import Data.Maybe (mapMaybe) import Text.ParserCombinators.ReadP import Text.Read (readMaybe) @@ -89,9 +87,6 @@ parseFieldNames = spaceSeparated parseFieldName parsePassport :: ReadP [Field] parsePassport = spaceSeparated parseField -splitOnEmptyLines :: String -> [String] -splitOnEmptyLines = map (intercalate " ") . filter (not . any null) . groupBy ((==) `on` null) . lines - hasAllRequiredFields :: String -> Bool hasAllRequiredFields = maybe False containsAllFields . fieldNames where requiredFieldNames = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]