From 4f13a47281e87bb439cd758c80ccf7393e3aea55 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sun, 6 Dec 2020 07:36:19 +0100 Subject: [PATCH] haskell: move splitOnEmptyLines to AoC.hs --- common/AoC.hs | 5 +++++ day4/day4.hs | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) 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"]