From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2021/day10/10a.hs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2021/day10/10a.hs (limited to '2021/day10/10a.hs') diff --git a/2021/day10/10a.hs b/2021/day10/10a.hs new file mode 100644 index 0000000..7dadbce --- /dev/null +++ b/2021/day10/10a.hs @@ -0,0 +1,50 @@ +module Main where + +isStartParen :: Char -> Bool +isStartParen '(' = True +isStartParen '[' = True +isStartParen '{' = True +isStartParen '<' = True +isStartParen _ = False + +isEndParen :: Char -> Bool +isEndParen ')' = True +isEndParen ']' = True +isEndParen '}' = True +isEndParen '>' = True +isEndParen _ = False + +flipParen :: Char -> Char +flipParen '(' = ')' +flipParen ')' = '(' +flipParen '[' = ']' +flipParen ']' = '[' +flipParen '{' = '}' +flipParen '}' = '{' +flipParen '<' = '>' +flipParen '>' = '<' +flipParen _ = undefined + +getScore :: Char -> Int +getScore ')' = 3 +getScore ']' = 57 +getScore '}' = 1197 +getScore '>' = 25137 + +consumeBlock :: Char -> String -> Either Char String +consumeBlock t "" = Right "" +consumeBlock t (c:cs) | c == flipParen t = Right cs + | isStartParen c = (consumeBlock c cs) >>= (consumeBlock t) + | otherwise = Left c + +corruptionScore :: String -> Int +corruptionScore "" = 0 +corruptionScore (c:cs) = case consumeBlock c cs of + Left c -> getScore c + Right s -> corruptionScore s + +main :: IO () +main = do + s <- readFile "./input" + let notCorrupted = foldr (+) 0 $ map corruptionScore (lines s) + print notCorrupted -- cgit v1.2.3