make day10 more maybe-oriented
This commit is contained in:
parent
832c62bef4
commit
862f7537b4
|
|
@ -4,12 +4,12 @@ import Data.Maybe
|
||||||
import Data.List (sort)
|
import Data.List (sort)
|
||||||
import Control.Monad (foldM)
|
import Control.Monad (foldM)
|
||||||
|
|
||||||
closing :: Char -> Char
|
closing :: Char -> Maybe Char
|
||||||
closing '(' = ')'
|
closing '(' = Just ')'
|
||||||
closing '[' = ']'
|
closing '[' = Just ']'
|
||||||
closing '{' = '}'
|
closing '{' = Just '}'
|
||||||
closing '<' = '>'
|
closing '<' = Just '>'
|
||||||
closing _ = error "Not a bracket"
|
closing _ = Nothing
|
||||||
|
|
||||||
score :: Char -> Maybe Int
|
score :: Char -> Maybe Int
|
||||||
score ')' = Just 3
|
score ')' = Just 3
|
||||||
|
|
@ -32,13 +32,13 @@ getInvalidChar :: [Char] -> String -> Char
|
||||||
getInvalidChar _ [] = ' '
|
getInvalidChar _ [] = ' '
|
||||||
getInvalidChar st (x:xs)
|
getInvalidChar st (x:xs)
|
||||||
| x `elem` "([{<" = getInvalidChar (x:st) xs
|
| x `elem` "([{<" = getInvalidChar (x:st) xs
|
||||||
| otherwise = if Just x == fmap closing (listToMaybe st) then getInvalidChar (tail st) xs else x
|
| otherwise = if Just x == (listToMaybe st >>= closing) then getInvalidChar (tail st) xs else x
|
||||||
|
|
||||||
getCompletionString :: [Char] -> String -> String
|
getCompletionString :: [Char] -> String -> String
|
||||||
getCompletionString st [] = map closing st
|
getCompletionString st [] = map (fromJust . closing) st
|
||||||
getCompletionString st (x:xs)
|
getCompletionString st (x:xs)
|
||||||
| x `elem` "([{<" = getCompletionString (x:st) xs
|
| x `elem` "([{<" = getCompletionString (x:st) xs
|
||||||
| otherwise = if Just x == fmap closing (listToMaybe st) then getCompletionString (tail st) xs else ""
|
| otherwise = if Just x == (listToMaybe st >>= closing) then getCompletionString (tail st) xs else ""
|
||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue