haskell: move splitOnEmptyLines to AoC.hs

This commit is contained in:
Xiretza 2020-12-06 07:36:19 +01:00
parent 98c1af4d58
commit 51aaea90dc
2 changed files with 5 additions and 5 deletions

View file

@ -1,5 +1,7 @@
module AoC where module AoC where
import Data.Function (on)
import Data.List (groupBy, intercalate)
import Text.ParserCombinators.ReadP import Text.ParserCombinators.ReadP
(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d (.:) :: (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 [(x, "")] -> Just x
_ -> Nothing _ -> 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 :: (Show r1, Show r2) => (String -> i) -> (i -> r1) -> (i -> r2) -> IO ()
runAoC inputTransform part1 part2 = do runAoC inputTransform part1 part2 = do
contents <- inputTransform <$> getContents contents <- inputTransform <$> getContents

View file

@ -3,8 +3,6 @@ import AoC
import Control.Applicative ((<|>)) import Control.Applicative ((<|>))
import Control.Monad (guard, mfilter) import Control.Monad (guard, mfilter)
import Data.Char (isDigit) import Data.Char (isDigit)
import Data.Function (on)
import Data.List (groupBy, intercalate)
import Data.Maybe (mapMaybe) import Data.Maybe (mapMaybe)
import Text.ParserCombinators.ReadP import Text.ParserCombinators.ReadP
import Text.Read (readMaybe) import Text.Read (readMaybe)
@ -89,9 +87,6 @@ parseFieldNames = spaceSeparated parseFieldName
parsePassport :: ReadP [Field] parsePassport :: ReadP [Field]
parsePassport = spaceSeparated parseField parsePassport = spaceSeparated parseField
splitOnEmptyLines :: String -> [String]
splitOnEmptyLines = map (intercalate " ") . filter (not . any null) . groupBy ((==) `on` null) . lines
hasAllRequiredFields :: String -> Bool hasAllRequiredFields :: String -> Bool
hasAllRequiredFields = maybe False containsAllFields . fieldNames hasAllRequiredFields = maybe False containsAllFields . fieldNames
where requiredFieldNames = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"] where requiredFieldNames = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"]