9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
15 "execution_count": 40,
19 "data ParseState = ParseState\n",
22 " , garbageCount :: Int\n",
23 " , readingGarbage :: Bool\n",
24 " , ignoreCharacter :: Bool\n",
25 " } deriving (Show, Eq)"
30 "execution_count": 41,
34 "openGroup ps = ps {depth = depth ps + 1}\n",
36 "closeGroup ps = ps {total = total ps + depth ps, depth = depth ps - 1}"
41 "execution_count": 42,
45 "-- parse ps c = if ignoreCharacter ps\n",
46 "-- then ps {ignoreCharacter = False}\n",
47 "-- else if readingGarbage ps \n",
48 "-- then if c == '>'\n",
49 "-- then ps {readingGarbage = False}\n",
51 "-- else case c of '<' -> ps {readingGarbage = True}\n",
52 "-- '{' -> openGroup ps\n",
53 "-- '}' -> closeGroup ps\n",
59 "execution_count": 44,
64 " | ignoreCharacter ps = ps {ignoreCharacter = False}\n",
65 " | c == '!' = ps {ignoreCharacter = True}\n",
66 " | readingGarbage ps = if c == '>'\n",
67 " then ps {readingGarbage = False}\n",
68 " else ps {garbageCount = garbageCount ps + 1}\n",
71 " '<' -> ps {readingGarbage = True}\n",
72 " '{' -> openGroup ps\n",
73 " '}' -> closeGroup ps\n",
79 "execution_count": 45,
83 "process = foldl parse ps0\n",
84 " where ps0 = ParseState {total = 0, depth = 0, garbageCount = 0,\n",
85 " readingGarbage = False, ignoreCharacter = False}"
90 "execution_count": 46,
100 "output_type": "display_data"
104 "score \"{{<ab>},{<ab>},{<ab>},{<ab>}}\""
109 "execution_count": 47,
119 "output_type": "display_data"
123 "score \"{{<!!>},{<!!>},{<!!>},{<!!>}}\""
128 "execution_count": 48,
138 "output_type": "display_data"
142 "score \"{{<a!>},{<a!>},{<a!>},{<ab>}}\""
147 "execution_count": 49,
151 "part1 = total . process"
156 "execution_count": 50,
160 "part2 = garbageCount . process"
165 "execution_count": 60,
171 " text <- readFile \"../../data/advent09.txt\"\n",
172 " print $ part1 text\n",
173 " print $ part2 text"
178 "execution_count": 61,
189 "output_type": "display_data"
198 "execution_count": 54,
208 "output_type": "display_data"
212 "part2 \"<random characters>\""
217 "execution_count": 55,
227 "output_type": "display_data"
236 "execution_count": 56,
246 "output_type": "display_data"
255 "execution_count": 57,
265 "output_type": "display_data"
274 "execution_count": 58,
284 "output_type": "display_data"
293 "execution_count": 59,
303 "output_type": "display_data"
307 "part2 \"<{o\\\"i!a,<{i<a>\""
312 "execution_count": null,
320 "display_name": "Haskell",
321 "language": "haskell",
325 "codemirror_mode": "ihaskell",
326 "file_extension": ".hs",