fixed up split in day 4

This commit is contained in:
Thorn Avery 2020-12-06 20:20:18 +13:00
parent c1c14a1fe9
commit f48f0459ac

17
day4.hs
View File

@ -9,7 +9,7 @@ import Data.List (sort, nub)
main :: IO ()
main = do
raw <- readFile "day4.txt"
let ls = parse $ lines raw
let ls = map (concat . (map words)) $ split "" $ lines raw
ansA = length $ filter (== True) $ map solveA ls
ansB = length $ filter (== True) $ map solveB ls
in do
@ -96,12 +96,9 @@ hgtValid s' =
s = drop 1 $ dropWhile (/= ':') s'
(val,unit) = head $ (reads s :: [(Int,String)])
parse :: [String] -> [[String]]
parse [] = []
parse s = parse' s [] []
parse' :: [String] -> [String] -> [[String]] -> [[String]]
parse' [] as bs = bs ++ [as]
parse' (s:ss) as bs
| s == "" = parse' ss [] $ bs ++ [as]
| otherwise = parse' ss (as ++ (words s)) bs
split :: Eq a => a -> [a] -> [[a]]
split _ [] = []
split d as = chunk : (split d rest)
where
chunk = takeWhile (/= d) as
rest = drop 1 $ dropWhile (/= d) as