Day 9
[advent-of-code-17.git] / src / advent09 / advent09.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
11 ]
12 },
13 {
14 "cell_type": "code",
15 "execution_count": 40,
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "data ParseState = ParseState\n",
20 " { total :: Int\n",
21 " , depth :: Int\n",
22 " , garbageCount :: Int\n",
23 " , readingGarbage :: Bool\n",
24 " , ignoreCharacter :: Bool\n",
25 " } deriving (Show, Eq)"
26 ]
27 },
28 {
29 "cell_type": "code",
30 "execution_count": 41,
31 "metadata": {},
32 "outputs": [],
33 "source": [
34 "openGroup ps = ps {depth = depth ps + 1}\n",
35 "\n",
36 "closeGroup ps = ps {total = total ps + depth ps, depth = depth ps - 1}"
37 ]
38 },
39 {
40 "cell_type": "code",
41 "execution_count": 42,
42 "metadata": {},
43 "outputs": [],
44 "source": [
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",
50 "-- else ps\n",
51 "-- else case c of '<' -> ps {readingGarbage = True}\n",
52 "-- '{' -> openGroup ps\n",
53 "-- '}' -> closeGroup ps\n",
54 "-- _ -> ps"
55 ]
56 },
57 {
58 "cell_type": "code",
59 "execution_count": 44,
60 "metadata": {},
61 "outputs": [],
62 "source": [
63 "parse ps c \n",
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",
69 " | otherwise = \n",
70 " case c of \n",
71 " '<' -> ps {readingGarbage = True}\n",
72 " '{' -> openGroup ps\n",
73 " '}' -> closeGroup ps\n",
74 " _ -> ps"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": 45,
80 "metadata": {},
81 "outputs": [],
82 "source": [
83 "process = foldl parse ps0\n",
84 " where ps0 = ParseState {total = 0, depth = 0, garbageCount = 0,\n",
85 " readingGarbage = False, ignoreCharacter = False}"
86 ]
87 },
88 {
89 "cell_type": "code",
90 "execution_count": 46,
91 "metadata": {},
92 "outputs": [
93 {
94 "data": {
95 "text/plain": [
96 "9"
97 ]
98 },
99 "metadata": {},
100 "output_type": "display_data"
101 }
102 ],
103 "source": [
104 "score \"{{<ab>},{<ab>},{<ab>},{<ab>}}\""
105 ]
106 },
107 {
108 "cell_type": "code",
109 "execution_count": 47,
110 "metadata": {},
111 "outputs": [
112 {
113 "data": {
114 "text/plain": [
115 "9"
116 ]
117 },
118 "metadata": {},
119 "output_type": "display_data"
120 }
121 ],
122 "source": [
123 "score \"{{<!!>},{<!!>},{<!!>},{<!!>}}\""
124 ]
125 },
126 {
127 "cell_type": "code",
128 "execution_count": 48,
129 "metadata": {},
130 "outputs": [
131 {
132 "data": {
133 "text/plain": [
134 "3"
135 ]
136 },
137 "metadata": {},
138 "output_type": "display_data"
139 }
140 ],
141 "source": [
142 "score \"{{<a!>},{<a!>},{<a!>},{<ab>}}\""
143 ]
144 },
145 {
146 "cell_type": "code",
147 "execution_count": 49,
148 "metadata": {},
149 "outputs": [],
150 "source": [
151 "part1 = total . process"
152 ]
153 },
154 {
155 "cell_type": "code",
156 "execution_count": 50,
157 "metadata": {},
158 "outputs": [],
159 "source": [
160 "part2 = garbageCount . process"
161 ]
162 },
163 {
164 "cell_type": "code",
165 "execution_count": 60,
166 "metadata": {},
167 "outputs": [],
168 "source": [
169 "main :: IO ()\n",
170 "main = do \n",
171 " text <- readFile \"../../data/advent09.txt\"\n",
172 " print $ part1 text\n",
173 " print $ part2 text"
174 ]
175 },
176 {
177 "cell_type": "code",
178 "execution_count": 61,
179 "metadata": {},
180 "outputs": [
181 {
182 "data": {
183 "text/plain": [
184 "11089\n",
185 "5288"
186 ]
187 },
188 "metadata": {},
189 "output_type": "display_data"
190 }
191 ],
192 "source": [
193 "main"
194 ]
195 },
196 {
197 "cell_type": "code",
198 "execution_count": 54,
199 "metadata": {},
200 "outputs": [
201 {
202 "data": {
203 "text/plain": [
204 "17"
205 ]
206 },
207 "metadata": {},
208 "output_type": "display_data"
209 }
210 ],
211 "source": [
212 "part2 \"<random characters>\""
213 ]
214 },
215 {
216 "cell_type": "code",
217 "execution_count": 55,
218 "metadata": {},
219 "outputs": [
220 {
221 "data": {
222 "text/plain": [
223 "3"
224 ]
225 },
226 "metadata": {},
227 "output_type": "display_data"
228 }
229 ],
230 "source": [
231 "part2 \"<<<<>\""
232 ]
233 },
234 {
235 "cell_type": "code",
236 "execution_count": 56,
237 "metadata": {},
238 "outputs": [
239 {
240 "data": {
241 "text/plain": [
242 "2"
243 ]
244 },
245 "metadata": {},
246 "output_type": "display_data"
247 }
248 ],
249 "source": [
250 "part2 \"<{!>}>\""
251 ]
252 },
253 {
254 "cell_type": "code",
255 "execution_count": 57,
256 "metadata": {},
257 "outputs": [
258 {
259 "data": {
260 "text/plain": [
261 "0"
262 ]
263 },
264 "metadata": {},
265 "output_type": "display_data"
266 }
267 ],
268 "source": [
269 "part2 \"<!!>\""
270 ]
271 },
272 {
273 "cell_type": "code",
274 "execution_count": 58,
275 "metadata": {},
276 "outputs": [
277 {
278 "data": {
279 "text/plain": [
280 "0"
281 ]
282 },
283 "metadata": {},
284 "output_type": "display_data"
285 }
286 ],
287 "source": [
288 "part2 \"<!!!>>\""
289 ]
290 },
291 {
292 "cell_type": "code",
293 "execution_count": 59,
294 "metadata": {},
295 "outputs": [
296 {
297 "data": {
298 "text/plain": [
299 "10"
300 ]
301 },
302 "metadata": {},
303 "output_type": "display_data"
304 }
305 ],
306 "source": [
307 "part2 \"<{o\\\"i!a,<{i<a>\""
308 ]
309 },
310 {
311 "cell_type": "code",
312 "execution_count": null,
313 "metadata": {},
314 "outputs": [],
315 "source": []
316 }
317 ],
318 "metadata": {
319 "kernelspec": {
320 "display_name": "Haskell",
321 "language": "haskell",
322 "name": "haskell"
323 },
324 "language_info": {
325 "codemirror_mode": "ihaskell",
326 "file_extension": ".hs",
327 "name": "haskell",
328 "version": "8.0.2"
329 }
330 },
331 "nbformat": 4,
332 "nbformat_minor": 2
333 }