Day 10
This commit is contained in:
parent
22fb204de0
commit
d15e8ec49c
|
@ -0,0 +1,33 @@
|
|||
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
|
|
@ -0,0 +1,35 @@
|
|||
module Day10_1 where
|
||||
import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents)
|
||||
import Data.Maybe (mapMaybe)
|
||||
import Data.List (sort)
|
||||
|
||||
closing :: Char -> Char
|
||||
closing '(' = ')'
|
||||
closing '[' = ']'
|
||||
closing '{' = '}'
|
||||
closing '<' = '>'
|
||||
|
||||
score :: Char -> Int
|
||||
score ')' = 1
|
||||
score ']' = 2
|
||||
score '}' = 3
|
||||
score '>' = 4
|
||||
|
||||
|
||||
matchBraces :: [Char] -> [Char] -> Maybe [Char]
|
||||
matchBraces [] [] = Nothing
|
||||
matchBraces acc [] = Just acc
|
||||
matchBraces acc (x:xs)
|
||||
| x `elem` ['(', '[', '{', '<'] = matchBraces (closing x : acc) xs
|
||||
| x == head acc = matchBraces (tail acc) xs
|
||||
| otherwise = Nothing
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- openFile "inputs/10.txt" ReadMode
|
||||
contents <- hGetContents input
|
||||
|
||||
let routes = lines contents
|
||||
print . (\x -> x !! (length x `div` 2)) . sort $ map (foldl (\acc x -> score x + (acc * 5)) 0) $ mapMaybe (matchBraces []) routes
|
||||
|
||||
hClose input
|
Loading…
Reference in New Issue