day16/haskell: readability improvements

This commit is contained in:
Xiretza 2020-12-16 17:56:44 +01:00
parent 873ea19b8d
commit 5bf3f4e0ce

View file

@ -53,7 +53,7 @@ parseInput = do
return $ Input {fields, myTicket, otherTickets } return $ Input {fields, myTicket, otherTickets }
matchesAnyField :: [Field] -> Int -> Bool matchesAnyField :: [Field] -> Int -> Bool
matchesAnyField fields n = any (n `S.member`) . map valids $ fields matchesAnyField fields n = any ((n `S.member`) . valids) fields
iterateUntilNothing :: (a -> Maybe a) -> a -> a iterateUntilNothing :: (a -> Maybe a) -> a -> a
iterateUntilNothing f x = maybe x (iterateUntilNothing f) $ f x iterateUntilNothing f x = maybe x (iterateUntilNothing f) $ f x
@ -75,10 +75,10 @@ findSingletonChoice (x:xs) =
resolve' :: Ord a => [Resolve a] -> Maybe [Resolve a] resolve' :: Ord a => [Resolve a] -> Maybe [Resolve a]
resolve' xs = do resolve' xs = do
(x, xs') <- findSingletonChoice xs (r, xs') <- findSingletonChoice xs
return $ map (dropChoice x) xs' return $ map (dropChoice r) xs'
where dropChoice x (Choice s) = Choice (S.delete x s) where dropChoice x (Choice s) = Choice (S.delete x s)
dropChoice _ r = r dropChoice _ y = y
part1 :: Input -> Int part1 :: Input -> Int
part1 Input { fields, otherTickets } = sum . filter (not . matchesAnyField fields) . concat $ otherTickets part1 Input { fields, otherTickets } = sum . filter (not . matchesAnyField fields) . concat $ otherTickets