make day10 more maybe-oriented

This commit is contained in:
Quinten Kock 2021-12-11 19:29:00 +01:00
parent 832c62bef4
commit 862f7537b4
1 changed files with 9 additions and 9 deletions

View File

@ -4,12 +4,12 @@ import Data.Maybe
import Data.List (sort)
import Control.Monad (foldM)
closing :: Char -> Char
closing '(' = ')'
closing '[' = ']'
closing '{' = '}'
closing '<' = '>'
closing _ = error "Not a bracket"
closing :: Char -> Maybe Char
closing '(' = Just ')'
closing '[' = Just ']'
closing '{' = Just '}'
closing '<' = Just '>'
closing _ = Nothing
score :: Char -> Maybe Int
score ')' = Just 3
@ -32,13 +32,13 @@ getInvalidChar :: [Char] -> String -> Char
getInvalidChar _ [] = ' '
getInvalidChar st (x: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 st [] = map closing st
getCompletionString st [] = map (fromJust . closing) st
getCompletionString st (x: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 ()