Compare commits
2 Commits
ffe10097c3
...
862f7537b4
| Author | SHA1 | Date |
|---|---|---|
|
|
862f7537b4 | |
|
|
832c62bef4 |
|
|
@ -2,18 +2,21 @@ module Day10 where
|
||||||
|
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.List (sort)
|
import Data.List (sort)
|
||||||
closing :: Char -> Char
|
import Control.Monad (foldM)
|
||||||
closing '(' = ')'
|
|
||||||
closing '[' = ']'
|
|
||||||
closing '{' = '}'
|
|
||||||
closing '<' = '>'
|
|
||||||
|
|
||||||
score :: Char -> Int
|
closing :: Char -> Maybe Char
|
||||||
score ' ' = 0
|
closing '(' = Just ')'
|
||||||
score ')' = 3
|
closing '[' = Just ']'
|
||||||
score ']' = 57
|
closing '{' = Just '}'
|
||||||
score '}' = 1197
|
closing '<' = Just '>'
|
||||||
score '>' = 25137
|
closing _ = Nothing
|
||||||
|
|
||||||
|
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
|
||||||
|
|
@ -28,14 +31,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 == '(' || x == '[' || x == '{' || x == '<' = getInvalidChar (x:st) 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 :: [Char] -> String -> String
|
||||||
getCompletionString st [] = map closing st
|
getCompletionString st [] = map (fromJust . closing) st
|
||||||
getCompletionString st (x:xs)
|
getCompletionString st (x:xs)
|
||||||
| x == '(' || x == '[' || x == '{' || x == '<' = getCompletionString (x:st) 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 ()
|
main :: IO ()
|
||||||
|
|
@ -43,7 +46,7 @@ main = do
|
||||||
input <- lines <$> getContents
|
input <- lines <$> getContents
|
||||||
|
|
||||||
putStr "part 1: "
|
putStr "part 1: "
|
||||||
let chars = map (score . getInvalidChar []) input
|
let chars = mapMaybe (score . getInvalidChar []) input
|
||||||
print $ sum chars
|
print $ sum chars
|
||||||
|
|
||||||
putStr "part 2: "
|
putStr "part 2: "
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue