From 862f7537b45e65423577b49958f54981ec9d9aa6 Mon Sep 17 00:00:00 2001 From: Quinten Kock Date: Sat, 11 Dec 2021 19:29:00 +0100 Subject: [PATCH] make day10 more maybe-oriented --- 2021/day10.hs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/2021/day10.hs b/2021/day10.hs index 5719650..2aab7d5 100644 --- a/2021/day10.hs +++ b/2021/day10.hs @@ -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 ()