1
0
Fork 0

Day 9 - placed 917 and 905 on the leaderboardgit add .

This commit is contained in:
Jonathan Chan 2017-12-08 22:01:23 -08:00
parent 8097d2578d
commit 512d3d1e8d
2 changed files with 50 additions and 0 deletions

49
9.hs Normal file
View File

@ -0,0 +1,49 @@
removeCancelled :: String -> String
removeCancelled str =
let (_, removed) = foldl (\(prev, rs) curr ->
case prev of
'!' -> ('\0', rs)
'\0' -> (curr, rs)
_ -> (curr, prev : rs))
(head str, "") (tail str)
in reverse $ filter (/= '\0') removed
removeGarbage :: String -> String
removeGarbage str =
let (_, removed) = foldl (\(isGarbage, rs) curr ->
if isGarbage
then (curr /= '>', rs)
else (curr == '<',
if curr == '<'
then rs
else curr : rs))
(False, "") str
in reverse $ filter (/= ',') removed
countNonGarbage :: String -> Int
countNonGarbage str =
let (_, removed) = foldl (\(isGarbage, rs) curr ->
if isGarbage
then (curr /= '>',
if curr == '>'
then rs
else curr : rs)
else (curr == '<', rs))
(False, "") str
in length removed
countGroups :: String -> Int
countGroups str =
let (_, total) = foldl (\(score, acc) curr ->
case curr of
'{' -> (score + 1, acc + score)
'}' -> (score - 1, acc))
(1, 0) str
in total
main :: IO ()
main = do
input <- readFile "9.txt"
print $ countGroups . removeGarbage . removeCancelled $ input
print $ countNonGarbage . removeCancelled $ input

1
9.txt Normal file

File diff suppressed because one or more lines are too long