From 6dee5caf29c2b17f70862f3bc0e6e6415d38ed20 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 16 Dec 2020 17:49:01 +0100 Subject: [PATCH] day16/haskell: prettify findSingletonChoice --- 2020/day16/day16.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/2020/day16/day16.hs b/2020/day16/day16.hs index 9ecf561..b5ccf44 100644 --- a/2020/day16/day16.hs +++ b/2020/day16/day16.hs @@ -66,11 +66,12 @@ resolve = sequence . map fromResolved . iterateUntilNothing resolve' . map (Choi fromResolved _ = Nothing findSingletonChoice :: [Resolve a] -> Maybe (a, [Resolve a]) -findSingletonChoice (c@(Choice s):xs) | S.size s == 1 = let [x] = S.elems s - in Just (x, Resolved x:xs) - | otherwise = (fmap.fmap) (c:) $ findSingletonChoice xs -findSingletonChoice (x:xs) = (fmap.fmap) (x:) $ findSingletonChoice xs -findSingletonChoice _ = Nothing +findSingletonChoice [] = Nothing +findSingletonChoice (x:xs) = + case x of + Choice s | S.size s == 1 -> let [r] = S.elems s + in Just (r, Resolved r : xs) + _ -> fmap (\(r, rest) -> (r, x:rest)) $ findSingletonChoice xs resolve' :: Ord a => [Resolve a] -> Maybe [Resolve a] resolve' xs = do