Advent of Code 2021
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
764 B

module Day10 where
import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents)
import Data.Maybe (mapMaybe)
closing :: Char -> Char
closing '(' = ')'
closing '[' = ']'
closing '{' = '}'
closing '<' = '>'
score :: Char -> Int
score ')' = 3
score ']' = 57
score '}' = 1197
score '>' = 25137
matchBraces :: [Char] -> [Char] -> Maybe Char
matchBraces _ [] = Nothing
matchBraces acc (x:xs)
| x `elem` ['(', '[', '{', '<'] = matchBraces (closing x : acc) xs
| x == head acc = matchBraces (tail acc) xs
| otherwise = Just x
main :: IO ()
main = do
input <- openFile "inputs/10.txt" ReadMode
contents <- hGetContents input
let routes = lines contents
print . sum . map score $ mapMaybe (matchBraces []) routes
hClose input