Compare commits

..

No commits in common. "862f7537b45e65423577b49958f54981ec9d9aa6" and "ffe10097c387a5cc59b8f97f55bf5151e4eece0a" have entirely different histories.

1 changed files with 17 additions and 20 deletions

View File

@ -2,21 +2,18 @@ module Day10 where
import Data.Maybe import Data.Maybe
import Data.List (sort) import Data.List (sort)
import Control.Monad (foldM) closing :: Char -> Char
closing '(' = ')'
closing '[' = ']'
closing '{' = '}'
closing '<' = '>'
closing :: Char -> Maybe Char score :: Char -> Int
closing '(' = Just ')' score ' ' = 0
closing '[' = Just ']' score ')' = 3
closing '{' = Just '}' score ']' = 57
closing '<' = Just '>' score '}' = 1197
closing _ = Nothing score '>' = 25137
score :: Char -> Maybe Int
score ')' = Just 3
score ']' = Just 57
score '}' = Just 1197
score '>' = Just 25137
score _ = Nothing
closingScore :: Char -> Int closingScore :: Char -> Int
closingScore ')' = 1 closingScore ')' = 1
@ -31,14 +28,14 @@ calcScore xs = foldr (\char score -> score*5 + closingScore char) 0 (reverse xs)
getInvalidChar :: [Char] -> String -> Char getInvalidChar :: [Char] -> String -> Char
getInvalidChar _ [] = ' ' getInvalidChar _ [] = ' '
getInvalidChar st (x:xs) getInvalidChar st (x:xs)
| x `elem` "([{<" = getInvalidChar (x:st) xs | x == '(' || x == '[' || x == '{' || x == '<' = getInvalidChar (x:st) xs
| otherwise = if Just x == (listToMaybe st >>= closing) then getInvalidChar (tail st) xs else x | otherwise = if Just x == fmap closing (listToMaybe st) then getInvalidChar (tail st) xs else x
getCompletionString :: [Char] -> String -> String getCompletionString :: [Char] -> String -> String
getCompletionString st [] = map (fromJust . closing) st getCompletionString st [] = map closing st
getCompletionString st (x:xs) getCompletionString st (x:xs)
| x `elem` "([{<" = getCompletionString (x:st) xs | x == '(' || x == '[' || x == '{' || x == '<' = getCompletionString (x:st) xs
| otherwise = if Just x == (listToMaybe st >>= closing) then getCompletionString (tail st) xs else "" | otherwise = if Just x == fmap closing (listToMaybe st) then getCompletionString (tail st) xs else ""
main :: IO () main :: IO ()
@ -46,7 +43,7 @@ main = do
input <- lines <$> getContents input <- lines <$> getContents
putStr "part 1: " putStr "part 1: "
let chars = mapMaybe (score . getInvalidChar []) input let chars = map (score . getInvalidChar []) input
print $ sum chars print $ sum chars
putStr "part 2: " putStr "part 2: "