1 data ParseState = ParseState
5 , readingGarbage :: Bool
6 , ignoreCharacter :: Bool
11 text <- readFile "data/advent09.txt"
16 part1 :: String -> Int
17 part1 = total . process
19 part2 :: String -> Int
20 part2 = garbageCount . process
23 process :: String -> ParseState
24 process = foldl parse ps0
25 where ps0 = ParseState {total = 0, depth = 0, garbageCount = 0,
26 readingGarbage = False, ignoreCharacter = False}
28 parse :: ParseState -> Char -> ParseState
30 | ignoreCharacter ps = ps {ignoreCharacter = False}
31 | c == '!' = ps {ignoreCharacter = True}
32 | readingGarbage ps = if c == '>'
33 then ps {readingGarbage = False}
34 else ps {garbageCount = garbageCount ps + 1}
37 '<' -> ps {readingGarbage = True}
42 openGroup :: ParseState -> ParseState
43 openGroup ps = ps {depth = depth ps + 1}
45 closeGroup :: ParseState -> ParseState
46 closeGroup ps = ps {total = total ps + depth ps, depth = depth ps - 1}