Commit acf2328f by Stuart Kurtz

Day 10

parent c3bfe3c7
This diff is collapsed. Click to expand it.
module Main where
score :: String -> Int
score = go [] where
match [] _ = False
match (a:_) c = (a,c) `elem` [('(',')'), ('[',']'),('{','}'),('<','>')]
isOpen c = c `elem` ['(','[','{','<']
go _ [] = 0 -- out of input
go as (c:cs)
| isOpen c = go (c:as) cs
| match as c = go (tail as) cs
| otherwise = charScore c
charScore ')' = 3
charScore ']' = 57
charScore '}' = 1197
charScore '>' = 25137
charScore c = error $ "score.charScore -- unexpected character: " ++ [c]
main :: IO ()
main = pure ()
main = do
input <- lines <$> readFile "data/syntax.txt"
print . sum . map score $ input
module Main where
import Data.List (sort)
import Data.Maybe (mapMaybe)
score :: String -> Maybe Int
score = go [] where
match [] _ = False
match (a:_) c = (a,c) `elem` [('(',')'), ('[',']'),('{','}'),('<','>')]
isOpen c = c `elem` ['(','[','{','<']
go as [] = Just $ stackScore as
go as (c:cs)
| isOpen c = go (c:as) cs
| match as c = go (tail as) cs
| otherwise = Nothing -- bad syntax
stackScore = foldl (\s c -> charScore c + 5 * s) 0
charScore '(' = 1
charScore '[' = 2
charScore '{' = 3
charScore '<' = 4
charScore c = error $ "score.charScore -- unexpected character: " ++ [c]
middle :: [a] -> a
middle as = head . drop (length as `div` 2) $ as
main :: IO ()
main = pure ()
main = do
input <- lines <$> readFile "data/syntax.txt"
putStr . show . middle . sort . mapMaybe score $ input
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment