faster Solution for 5
This commit is contained in:
parent
3db4dc2e9b
commit
780d339a68
14
5/5.hs
14
5/5.hs
@ -25,20 +25,20 @@ simplify acc s
|
|||||||
then simplify acc (S.drop 2 s)
|
then simplify acc (S.drop 2 s)
|
||||||
else simplify (acc |> x) (S.drop 1 s)
|
else simplify (acc |> x) (S.drop 1 s)
|
||||||
|
|
||||||
collapse :: S.Seq Char -> Int
|
collapse :: S.Seq Char -> S.Seq Char
|
||||||
collapse s =
|
collapse s =
|
||||||
let simplified = simplify S.empty s
|
let simplified = simplify S.empty s
|
||||||
in if S.length simplified == S.length s
|
in if S.length simplified == S.length s
|
||||||
then S.length s
|
then s
|
||||||
else collapse simplified
|
else collapse simplified
|
||||||
|
|
||||||
betterCollapse :: S.Seq Char -> (Char, Int)
|
betterCollapse :: S.Seq Char -> S.Seq Char
|
||||||
betterCollapse s =
|
betterCollapse s =
|
||||||
minimumBy (compare `on` snd)
|
minimumBy (compare `on` S.length)
|
||||||
. map (\c -> (c, collapse $ S.filter (\c' -> toLower c' /= toLower c) s))
|
. map (\c -> collapse $ S.filter (\c' -> toLower c' /= toLower c) s)
|
||||||
$ ['a' .. 'z']
|
$ ['a' .. 'z']
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
input <- S.fromList . filter (/= '\n') <$> getContents
|
input <- S.fromList . filter (/= '\n') <$> getContents
|
||||||
putStrLn $ "Size = " ++ show (collapse input)
|
putStrLn $ "Size = " ++ show (S.length $ collapse input)
|
||||||
putStrLn $ "Better Size = " ++ show (betterCollapse input)
|
putStrLn $ "Better Size = " ++ show (S.length $ betterCollapse (collapse input))
|
Loading…
Reference in New Issue
Block a user