{-
Copyright (C) 2017 Yuchen Pei.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
.
-}
-- Part 1 is adapted from https://github.com/ephemient/aoc2017/blob/master/src/Day9.hs
import Data.List (mapAccumL)
data State = Group | Garbage | Ignore
f :: (State, Int) -> Char -> ((State, Int), Int)
f (Group, n) '{' = ((Group, n + 1), n + 1)
f (Group, n) '}' = ((Group, n - 1), 0)
f (Group, n) '<' = ((Garbage, n), 0)
f (Garbage, n) '>' = ((Group, n), 0)
f (Garbage, n) '!' = ((Ignore, n), 0)
f (Ignore, n) _ = ((Garbage, n), 0)
f x _ = (x, 0)
solve1 :: [Char] -> Int
solve1 = sum . snd . (mapAccumL f (Group, 0))
g :: (State, Int) -> Char -> (State, Int)
g (Group, n) '<' = (Garbage, n)
g (Group, n) _ = (Group, n)
g (Garbage, n) '>' = (Group, n)
g (Garbage, n) '!' = (Ignore, n)
g (Garbage, n) _ = (Garbage, n + 1)
g (Ignore, n) _ = (Garbage, n)
solve2 :: [Char] -> Int
solve2 = snd . (foldl g (Group, 0))
input = "{{{{{{},>}},{{{{{{{!!!>!!}<>},{}}},},,!>!>,},},<>},{,}},{}}},{{{{,<{>},{}},{{{aa>},{<,oi!!!>aa,!!!},}},{<\"a!>!a,{\"!o'!>,,,i>,{}}}}}},{{{{{{!!!>,<{!>},,,<<\">},{{a!>!!!{\"{ao!>},aa!!!>!>},<\"!>,!>,<>},!!o!>,},{<}!'i,!!}e!>,},!'>,{}}},{{{{{{{,o!!!><\",i!>,,uuao>}},{<}<\"a{!!!>!!!>,u!>,>,<\"!!!!<}!u!o}'!!e!!!>a!!},,!>}!!>}},{<\">,,<>},{,<}!!{!>},,>,{,,<>}}},{{{<}!!{!!!>i,}'!\"ooo,!!>}},{}},{{{,'!>!!!>},<{!>},<{},}}}},{{<\"',,',a{i!oi,>}}},{{!!'}e!>{,!!!!!>\"a!>},<\"!>},<},!!i>}}},{{{{<\"{!>!!u!{\"\">,{<>}},{{{}},<,!>,},<>}},{{<{!>,,,<<},>},{}},{,,},<{>,{<,!>,},},\"eu!u!>},<{>}}}},{{{,!!}o>,{},<<>}},{{},,<\"!!!!!>!>,!>\",<\"io>},{}},{{,\">,<\">},{{<}{>,{{,{{{},},<,\"!>,!>!>,},}},{{aa\"!>oa\"!!!>!>>},{}}}},},}},{{{<{}}\">},{<{\"!>,!!'}!!!>},}},{u}}!,u!!!!!>!!,e!!!>\"!!!!'}!!!>},<{>},{{{a!,\"!>,<,}ua'!a>,{{!!\",}'!!i<<>},{'!>},}}}},<!!!>>}},{}},{{{},o!!>}},,>}},{{{{},<,,<}!'>},{{{{{{},<'{,!>},},},<<>},!!!>,}},<'}!>},!>},<<}!!\"!,!!!>,},{{}}},{<'!!!!!a'!!oa>,<\"{\"!!,!e!!}<'!>}!!!>i>},{<,!>},<{}!>},},,>,{<!,!\"!!!!,!!!>{!>},>}}},{},<\"\"!>,},,<>,<{!>i{!!!>!>!!{!'ee>}}}}},{{{<\"\"!}<!>!,!>},,>}},{{},!e!>!!!>ui!!,,!!!!!!!!!>!!>}},{{{{{<'{!!!!i'!>,!>>},},{{},<,{!!!>\"!>},<,!!!>,<'!}o>}},}!!!>e\"!>,<{{!>,,,!!!>>},{<,!>,}}},{},{{{}},{<>}},{{{{{ii\"!>,<\"}}},{{{}},{}},{,<,i!>},<>,{,<,,<'!>'>}}},{{<}u>,{}},{}},{{{},{!{!!!>},}},{{<\"ooaa<,!a!>},}}},{{{}},{{,{\"e}!!,!!!\"!!,!!!>,},},}},{{},{!u!'!e!o!>,,<<'>}},{{{{}},{!>'!!!>!!!!!!!>!!!>,<>,{,<'},!ii!\"<>}}}}},{{{{{},}!!!>},<{e!!!'{!>},,<<<>},}},{},{}},{{{{}},{,<},,uiie'!>},!!}>}},{{},{<>}},{{!!!!!>,}a>,<{!!>},{<>,},<>},{{}}}}}}},{{{{'!!!>a!>,,},{{<>}}},{{},},,<},<},!>},<>},{<>,,!>},,}},{{{<,},{}}},{{{{}},{{{{!>},<\"}!>'!>},!!<'}i!>!!>},{{{,{}}},{<}!!!>\"!a,u!!'ou!>}!a>}}}},{{},{<}!ae!>,<\"!>,{,>}}},{}}},{{{{,>},},!!!>,<,!>,},}}},{{{,a!!!>}}!>},,},<>,,},},{{{!!\"a\"'}>},{,<,,e!>i{>}},{<>}},{{},{{,!!!>!!!>i!>,<>,{}},,!!ii'!!>}\">},{<}a<,!o>}},{{{{},<,e!!!!!!!u>}},{},{{<\"'<'>,{!!{>}},{<'\"!!!!!>'!>\"{!>!>},},},{}}},{{{},'e!>},e\"!!!>,},<}eo!>,,<,a>}},{},{{}ie!!}!!i',>},},{{!!!!'!>}a\"!!!>,>}},{{{,<{aa'{oa!}!!!>i>,<,,!>\"!!!>!>,<}'!'i>},{,<>},{u!!!\"a,\"!>},ea!!!!}!>,}},{{},{{{<,o\"'>}}},{{!!!>!>{,'ue{'!!!>>}}}},{{{},o}!>,,'\"!!!!}},{{{{a!>},<''!!o!!!!!>ii!>,>},{<'\"!!!!{!!!!!>!>},<}>}},{{,,,<!>},<>}}}}},{{{{{{{}},{{{{{<{!>ie,!',!>,}a!e!!!!'!!>},{,},!!!!{>}},{},<,\"{!!!>'o}!>,<'{>,{<>}},{{},{}}}},{{a'!!!>,,},},!o'{iei!!u!>o{'\"!>},<>}}},{<{!!\"u>,<}!>,<>}}},{{,<>}}},{{<{!!,!o!!!>}\"!!!>!>},!>},,<'!a!!!>},<,!',{i>},{{},,<'!!<>}},{}},{}}}},{{{{{},{}},{!>,}},{{{<>}},{{o!>},!>},<>},{{},u\"<<,'!>>},{<'!!!>\"!>,,},>}}}},{{{{{},,},<>,{}},{{,{}},}i!!'u!e!!!!'!>,,<,!>,<>},{{{!!!>a}uiu!!>}},{,!!!>,<'!!!>o>}}}},{{{},{{},<{\"o>},{{},{{{}},{,<,!>,u!!ii!>>,{<'!!!>!!!'!!i{!'<>}},{,!>!>},,{}}},{,<>,{!!\"}!>},!'ei>}}}}},{},{{{<{!!!>>,{,<'<>}},{{i!!ae,,<}!!}>},{<,!!!>!>i>}},{{{{<,eo}!}o!>},<\"!!'<{,},!'ii,}!!!!e>},{!!!i!>,},e!!!>{!!{u!>},}},<,!!{uo<'!>!!\"}}>}},{{{},{{!\"o},{,<\"!!!u!>!>,!>,>}},{<{'e{!!\"!!}!!\"a!!!>i>}},{{<'!<,,}>,{!>},},,,e!!\"eea!>ie>}},{},{}}},{{<\"{!>},,<}!!!>!>,!>,<>},{{'!>!!'!>,\"io!!,!!}{!!!>,<>}},{{{,>},{{<\"!!!!!!}!>,,{},<',!>},<'!!!>,},<,!!!>!!e!!!>},<}},{}},{}},{{{{{{},<'>}}},{}},{{!>!!!>!!o>},{<,u\"!>},<'{!!!>{}{>}}}},{{},{<u!!,!}}}},{{{{{,<\"!!!>a!!!>{u!!!>>}}}},{{},{{!!!!}>,{},<,\",e'\"!!!>!!!>!>},}},{{!>!!!>u!>},<\"e<,{}},{{},<{!>,ou{!!,!!i<{o'i!>,}},{{{{},},!>},},{{{>}}}},{,,<{!>io,!\"\"o!!e\"\"aee>,}},{{<'ue{'u!!!>,!!u!>,!>o!o!!{>,,!>,},{{}},{{{},}},{}}},{{{}},{{,>}}},{{<''!!,!!\">,{<!>,,!>i>}},{{<,}u!!!>},}}}},{{{!>,,,o{!!>},!>},,!>},,!>uo>},{o!!!!a!>!!,u!!a!!!>!!!!!>\"!!!!!>>,{}},{{},!!\"!>},',}}}},{{{{},>,<>},{<>}},{}},{{}},{}},{{{{<,u!<\"},,}a!ou!'!>},<>},{{},!!a!>}},{{},{}}},{{{{{{},{}}},{{{},<}a\"aaa!>\"o!>,<,!>},<{!!!>!>,<{>}}}},{{{,<!!!>!!a,\"},,{{!!'!u<>}}},{!!!>!!i!!!>!!\"{!!!!!>>},{{!>},,},>}},{,}},{{{{{{<{uu<,o''}!!!!!!!ao,<>},{{},{{<,a!!!>!>!>,<\"u,<,!!!>!!uue{!>,<>}}}},},{{},<'e{e!!!>!},,},{{<},<>}}},{{!>},,}!!i!!!>},,},{{{},{}}},{<}!!a'\"'>,{{{{},{}},{},}},{ao!!''!!!>,},<,!!!>!>},}}}},{{a>},{{,,,!!i!>},<>}}}},{},{{<,ei'{,ea!!uo!>,,<>,<,!>{!!!!!!!>>},{},{{!!!>!>,<>}}},{{{<,i}u!!},!!!>{>},{{{!>,!>}>},},{{{{},}o{!!!>!!!>u!!ioe!!!!!>!!!>},<>}},{{}},{<}!!!!'\"'a!!{!>,<!!o',!>,!{!>,,<>,{},}}},{{{{!!!>!!!!!>ao!>,<\"{!>,e'!>,<>}},{{{!!}<<,>,{<}<'''!,!!!>ea!!'!{oi!!!!ao!{\">}},i>}},{{,,<}{e!!'!!!>u{!\"!!'>,{{},{\">}}},{{{<>}},{}}}},{,,e'!>,,{,!>!>,\"!>!>!}\"o'!!!>!e>}},{{<'{{'>}}}}}}},{{},!>},<,\"'ia!!!!!}e}>,{<>}}},{}},{{},!>,},!>,,,<},ou!!,!>,oi!!}>},{},{ae!!!>!>},<>}}},{{,,},,>}},{{{<,!!e!!'a!!'o!!!>,},{}},{<>}}}}}},{{{{{{{,,<{>}},<>},{},e!>},<',>}},{{},<}\">},{,!!!>'a,!>,<}!}'!!',,},}},{{{{,!!!!!>ao>},},<>},<}aa,uu>},{{},{},}}},{{},,{{{{<}!!!\"!>,o>}},{!>>}},{}}},{}}},{}},{{{{},{{<\"'\"uei{'a!u!!!>},},<\"u!!!>},<{!!a!>i!!e!!!>>}}},{{},{{{!!!!\"u{!>,<}>}}},{'!>,',!!!>oe\"'uu{oa}<>}},{{{<>,{{<,!!!<>},{}}},{{},<}}},{{,<'!!e!!!!!!!>,,<\"u!>i!>}!!},{{<\"a!>,},!!!>\"!>o!!!>u!>},},<>}}}}},{{{},,<,'{oi,}ia>,{{{{{{{},},a'{i'i>}}}},},<\"}i<}!!!>!!!>i>}}}},{{a>},{{!>},}}}},{{{}}},{{{{}},{{{eua{}}!>},},<}!>},},{<,!!}\"ei!!\">,<},,}},{}}},{{{{>},},},<,{>}},{<}!!e>,,!>},},{{<,!!e!!!>,,{!!}}!!},o!>},}}}},{{{{{},{}},{,,<\"!!!\"!}!>},',,},{{},{}!><>}}},{{{<\"\"!>!>,},!!!><{!!!>!!e>}},{{,!!<'i>}},{i,!>!!,i'a>,,,,<>}},{{},{{{,o,!!}!>!>!!i!!{u!>},<>},<\"!>},,<{!>!>},<!{!!!u}e!>},},<\"!,!\">},{{},,<>},{{<>,{}}!!,!>,}},{,{!>,,!i!!!>>}}},{}}}},{{{{,>},<>},{<{o\"{e!>,>,{<>}},{<{!>},},},<}u{,<{,!!!>!!<>}},{{!!i'>},{},{{{}},{{{a!,!!!>},},>},{{{}i,e!u>}},,'o!>,<}'!>,}},{},,}},{{{<>},,!!}!iaue!>!\">},{{!!!>},,!>>},{{{},{}},{!!!>o!>},,{\",},},,}},{},<>,{<>}}},{,,<!!{ui{>}},{{!>},!!!a'},<}!>>}}}},{{{{{{{{,<\"{!>,,{}},{{,!!!>},<''e!<>}}}},{<},o!!i!!!'!>,o,!}e!!!>,!>a>}},{}},{{},!!!>u!>!\"i{!>,},<'!>,<>}},{},{<>,{<\"!>,},,<\"!!!>!>,e{!!!>!!!!!!ue<>}}}}}},{{{},},{{oa!!o,},<<'!{>},{},e!!i,\"}!>!>},<>}}},{{{{{{{<}!'!!}!!e!>,<>}},{}},{}!!!>\"!>},<>},{{,i,>,}}},{{},{}}},{{{o\"uu>,{ii!i<>},<'oe{!>>},{<<'ao!!u!!e!!!!!>,}},{{{{{},,<{!ue'io!!aa,>},{}}},{{{{<'!!!>!>},>}},{<'!{},},{<,!>,<{>,{<}>}}},{},},{{{{},},},{{{o!>!!a!!'\"!>!!!!!>!>\"!{!>},<>}},,!!!>>}}},{{}},{{,!>!\"!>a\">,{<}>}},{}}}},{{},{},\"\",{\"i!!!>u\"a!!,>}}},{{{{{{},{{{},<'!'!>},}}},{},{ooiu!>},>}}},{{},<{!!,ue!>,\"!'!!'u!\"i!>,<>},{{},{!!'ueo>}},{{{<{!>!!a!!!>!!!>'!>'!!},},},{{},<'i!!!>!!o\"!}!u,i\"oeu\"\",>}}},},<},<}o\"i!>},<>}}},{{{{{{{!!!>},},<>}}},<!>}\"!>,},{}},{{,},e'\"}>}}},{{{<'!>},\">},{}},{}}},{{{{{,!!!>!!!!!!!>!u!>>},{},},}},{{<}o}!,!>},},<<<,,<>},{<,!!!>,}},{}},{{{\"uii!>},!!!>>},<,>}},{}},{{{<,{,,!>,,{{}}},{{<}!!a!{!!!>oi'a!>,<{{,'>},{<{o!!,!>,e,o}u!>},<>}}},{{,<'o!!!>'!!}!!\"{ue\"!>!!,>},,!!\"oie>},{{},}ei>}},{{,<,!!a}>}},{{{},{}},{{{<!>},,<}i!>e}o!>,,'>}}},{{{{<}!>,<>},{{{},{<>}}}},{}},{{{,{<{{\"!!>}},{{{{,u!!eao}'!!!>},},}!}>},{!>ui>}},{{'uo!>!!!!!>},!!!>>},{<,<'!>},},!!<>,<,{!>eu>}},{{<>},{}}},{{!>},,!!u}!!!>o!>},<>},{}}}}},{{{<\"!>>},{}}}}}}},{{<>,<'!>!>,!>},!<>},{{<,o'\"ia!>e}i,ie,u{!>},},}},{,},,},'o!!!>!>!>,<{\"i{!!}},{{},{{},,,<\"!>},<,>},{}},{{<\"oi>,,<,oi\"euuoo!>!>,,}}}}},{{},{{}},{<>}},{{{},<'>}}},{{{{},oiae},},}u!>>},{{,a!!!>i\"!!\"!!'>,{{},{}}}},{{!!\"!\"!e!a!!!>i!>},},<},,<>}},{{{!>,},<>},{!!!>!!!>},},e!!\"!>,<>}},<>},{{},{}}}},{{<,eo!!!>}!>,},<>,!!!>}e{!!!!!>!!\"u<>},{},{<<},!!!>u!>},},<>,!>},,i!!!>\"!!!>!>},}}},{{{!>},,},,>,{{,<,}!!\"ei!i!!,a\"'!>e,},<>}},{{{{<<,!>},'!>,,!!!eu{!>>}},,,{!!!!>},{