Done day 12
[advent-of-code-17.git] / src / advent11 / advent11.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": 2,
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "import Data.List.Split (splitOn)"
20 ]
21 },
22 {
23 "cell_type": "code",
24 "execution_count": 3,
25 "metadata": {},
26 "outputs": [],
27 "source": [
28 "hexStep :: (Int, Int) -> String -> (Int, Int)\n",
29 "hexStep (n, ne) s = case s of \n",
30 " \"n\" -> (n + 1, ne)\n",
31 " \"ne\" -> (n, ne + 1)\n",
32 " \"nw\" -> (n + 1, ne - 1)\n",
33 " \"s\" -> (n - 1, ne)\n",
34 " \"se\" -> (n - 1, ne + 1)\n",
35 " \"sw\" -> (n, ne - 1)"
36 ]
37 },
38 {
39 "cell_type": "code",
40 "execution_count": 4,
41 "metadata": {},
42 "outputs": [],
43 "source": [
44 "hexPath = foldl hexStep (0, 0)"
45 ]
46 },
47 {
48 "cell_type": "code",
49 "execution_count": 5,
50 "metadata": {},
51 "outputs": [
52 {
53 "data": {
54 "text/html": [
55 "<style>/* Styles used for the Hoogle display in the pager */\n",
56 ".hoogle-doc {\n",
57 "display: block;\n",
58 "padding-bottom: 1.3em;\n",
59 "padding-left: 0.4em;\n",
60 "}\n",
61 ".hoogle-code {\n",
62 "display: block;\n",
63 "font-family: monospace;\n",
64 "white-space: pre;\n",
65 "}\n",
66 ".hoogle-text {\n",
67 "display: block;\n",
68 "}\n",
69 ".hoogle-name {\n",
70 "color: green;\n",
71 "font-weight: bold;\n",
72 "}\n",
73 ".hoogle-head {\n",
74 "font-weight: bold;\n",
75 "}\n",
76 ".hoogle-sub {\n",
77 "display: block;\n",
78 "margin-left: 0.4em;\n",
79 "}\n",
80 ".hoogle-package {\n",
81 "font-weight: bold;\n",
82 "font-style: italic;\n",
83 "}\n",
84 ".hoogle-module {\n",
85 "font-weight: bold;\n",
86 "}\n",
87 ".hoogle-class {\n",
88 "font-weight: bold;\n",
89 "}\n",
90 ".get-type {\n",
91 "color: green;\n",
92 "font-weight: bold;\n",
93 "font-family: monospace;\n",
94 "display: block;\n",
95 "white-space: pre-wrap;\n",
96 "}\n",
97 ".show-type {\n",
98 "color: green;\n",
99 "font-weight: bold;\n",
100 "font-family: monospace;\n",
101 "margin-left: 1em;\n",
102 "}\n",
103 ".mono {\n",
104 "font-family: monospace;\n",
105 "display: block;\n",
106 "}\n",
107 ".err-msg {\n",
108 "color: red;\n",
109 "font-style: italic;\n",
110 "font-family: monospace;\n",
111 "white-space: pre;\n",
112 "display: block;\n",
113 "}\n",
114 "#unshowable {\n",
115 "color: red;\n",
116 "font-weight: bold;\n",
117 "}\n",
118 ".err-msg.in.collapse {\n",
119 "padding-top: 0.7em;\n",
120 "}\n",
121 ".highlight-code {\n",
122 "white-space: pre;\n",
123 "font-family: monospace;\n",
124 "}\n",
125 ".suggestion-warning { \n",
126 "font-weight: bold;\n",
127 "color: rgb(200, 130, 0);\n",
128 "}\n",
129 ".suggestion-error { \n",
130 "font-weight: bold;\n",
131 "color: red;\n",
132 "}\n",
133 ".suggestion-name {\n",
134 "font-weight: bold;\n",
135 "}\n",
136 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) + (abs ne)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">abs n + (abs ne)</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) + (abs ne)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) + abs ne</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs n) - smallest</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">abs n - smallest</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(abs ne) - smallest</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">abs ne - smallest</div></div>"
137 ],
138 "text/plain": [
139 "Line 2: Redundant bracket\n",
140 "Found:\n",
141 "(abs n) + (abs ne)\n",
142 "Why not:\n",
143 "abs n + (abs ne)Line 2: Redundant bracket\n",
144 "Found:\n",
145 "(abs n) + (abs ne)\n",
146 "Why not:\n",
147 "(abs n) + abs neLine 5: Redundant bracket\n",
148 "Found:\n",
149 "(abs n) - smallest\n",
150 "Why not:\n",
151 "abs n - smallestLine 5: Redundant bracket\n",
152 "Found:\n",
153 "(abs ne) - smallest\n",
154 "Why not:\n",
155 "abs ne - smallest"
156 ]
157 },
158 "metadata": {},
159 "output_type": "display_data"
160 }
161 ],
162 "source": [
163 "distance (n, ne) = if n * ne > 0 \n",
164 " then (abs n) + (abs ne)\n",
165 " else smallest + remainder\n",
166 " where smallest = min (abs n) (abs ne)\n",
167 " remainder = max ((abs n) - smallest) ((abs ne) - smallest)"
168 ]
169 },
170 {
171 "cell_type": "code",
172 "execution_count": 6,
173 "metadata": {},
174 "outputs": [
175 {
176 "data": {
177 "text/plain": [
178 "3"
179 ]
180 },
181 "metadata": {},
182 "output_type": "display_data"
183 }
184 ],
185 "source": [
186 "distance $ hexPath $ splitOn \",\" \"ne,ne,ne\""
187 ]
188 },
189 {
190 "cell_type": "code",
191 "execution_count": 7,
192 "metadata": {},
193 "outputs": [
194 {
195 "data": {
196 "text/plain": [
197 "0"
198 ]
199 },
200 "metadata": {},
201 "output_type": "display_data"
202 }
203 ],
204 "source": [
205 "distance $ hexPath $ splitOn \",\" \"ne,ne,sw,sw\""
206 ]
207 },
208 {
209 "cell_type": "code",
210 "execution_count": 8,
211 "metadata": {},
212 "outputs": [
213 {
214 "data": {
215 "text/plain": [
216 "2"
217 ]
218 },
219 "metadata": {},
220 "output_type": "display_data"
221 }
222 ],
223 "source": [
224 "distance $ hexPath $ splitOn \",\" \"ne,ne,s,s\""
225 ]
226 },
227 {
228 "cell_type": "code",
229 "execution_count": 9,
230 "metadata": {},
231 "outputs": [
232 {
233 "data": {
234 "text/plain": [
235 "3"
236 ]
237 },
238 "metadata": {},
239 "output_type": "display_data"
240 }
241 ],
242 "source": [
243 "distance $ hexPath $ splitOn \",\" \"se,sw,se,sw,sw\""
244 ]
245 },
246 {
247 "cell_type": "code",
248 "execution_count": 10,
249 "metadata": {},
250 "outputs": [],
251 "source": [
252 "part1 = distance . hexPath . splitOn \",\""
253 ]
254 },
255 {
256 "cell_type": "code",
257 "execution_count": 11,
258 "metadata": {},
259 "outputs": [],
260 "source": [
261 "main :: IO ()\n",
262 "main = do \n",
263 " text <- readFile \"../../data/advent11.txt\"\n",
264 "-- let instrs = map read $ splitOn \",\" text\n",
265 " print $ part1 text\n",
266 "-- print $ part2 text"
267 ]
268 },
269 {
270 "cell_type": "code",
271 "execution_count": 12,
272 "metadata": {},
273 "outputs": [
274 {
275 "data": {
276 "text/plain": [
277 "670"
278 ]
279 },
280 "metadata": {},
281 "output_type": "display_data"
282 }
283 ],
284 "source": [
285 "main"
286 ]
287 },
288 {
289 "cell_type": "code",
290 "execution_count": 13,
291 "metadata": {},
292 "outputs": [],
293 "source": [
294 "hexPathB = scanl hexStep (0, 0)"
295 ]
296 },
297 {
298 "cell_type": "code",
299 "execution_count": 14,
300 "metadata": {},
301 "outputs": [
302 {
303 "data": {
304 "text/plain": [
305 "3"
306 ]
307 },
308 "metadata": {},
309 "output_type": "display_data"
310 }
311 ],
312 "source": [
313 "maximum $ map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\""
314 ]
315 },
316 {
317 "cell_type": "code",
318 "execution_count": 15,
319 "metadata": {},
320 "outputs": [
321 {
322 "data": {
323 "text/plain": [
324 "[(0,0),(0,1),(0,2),(0,3)]"
325 ]
326 },
327 "metadata": {},
328 "output_type": "display_data"
329 }
330 ],
331 "source": [
332 "hexPathB $ splitOn \",\" \"ne,ne,ne\""
333 ]
334 },
335 {
336 "cell_type": "code",
337 "execution_count": 16,
338 "metadata": {},
339 "outputs": [
340 {
341 "data": {
342 "text/plain": [
343 "[0,1,2,3]"
344 ]
345 },
346 "metadata": {},
347 "output_type": "display_data"
348 }
349 ],
350 "source": [
351 "map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\""
352 ]
353 },
354 {
355 "cell_type": "code",
356 "execution_count": 17,
357 "metadata": {},
358 "outputs": [
359 {
360 "data": {
361 "text/plain": [
362 "([(0,0),(0,1),(0,2),(0,3)],[0,1,2,3],3)"
363 ]
364 },
365 "metadata": {},
366 "output_type": "display_data"
367 }
368 ],
369 "source": [
370 "pth = splitOn \",\" \"ne,ne,ne\"\n",
371 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
372 ]
373 },
374 {
375 "cell_type": "code",
376 "execution_count": 18,
377 "metadata": {},
378 "outputs": [
379 {
380 "data": {
381 "text/plain": [
382 "([(0,0),(0,1),(0,2),(0,1),(0,0)],[0,1,2,1,0],2)"
383 ]
384 },
385 "metadata": {},
386 "output_type": "display_data"
387 }
388 ],
389 "source": [
390 "pth = splitOn \",\" \"ne,ne,sw,sw\"\n",
391 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
392 ]
393 },
394 {
395 "cell_type": "code",
396 "execution_count": 19,
397 "metadata": {},
398 "outputs": [
399 {
400 "data": {
401 "text/plain": [
402 "([(0,0),(0,1),(0,2),(-1,2),(-2,2)],[0,1,2,2,2],2)"
403 ]
404 },
405 "metadata": {},
406 "output_type": "display_data"
407 }
408 ],
409 "source": [
410 "pth = splitOn \",\" \"ne,ne,s,s\"\n",
411 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
412 ]
413 },
414 {
415 "cell_type": "code",
416 "execution_count": 20,
417 "metadata": {},
418 "outputs": [
419 {
420 "data": {
421 "text/plain": [
422 "([(0,0),(-1,1),(-1,0),(-2,1),(-2,0),(-2,-1)],[0,1,1,2,2,3],3)"
423 ]
424 },
425 "metadata": {},
426 "output_type": "display_data"
427 }
428 ],
429 "source": [
430 "pth = splitOn \",\" \"se,sw,se,sw,sw\"\n",
431 "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)"
432 ]
433 },
434 {
435 "cell_type": "code",
436 "execution_count": 21,
437 "metadata": {},
438 "outputs": [],
439 "source": [
440 "part2 = maximum . map distance . hexPathB . splitOn \",\""
441 ]
442 },
443 {
444 "cell_type": "code",
445 "execution_count": 22,
446 "metadata": {},
447 "outputs": [],
448 "source": [
449 "main :: IO ()\n",
450 "main = do \n",
451 " text <- readFile \"../../data/advent11.txt\"\n",
452 "-- let instrs = map read $ splitOn \",\" text\n",
453 " print $ part1 text\n",
454 " print $ part2 text"
455 ]
456 },
457 {
458 "cell_type": "code",
459 "execution_count": 23,
460 "metadata": {},
461 "outputs": [
462 {
463 "data": {
464 "text/plain": [
465 "670\n",
466 "1426"
467 ]
468 },
469 "metadata": {},
470 "output_type": "display_data"
471 }
472 ],
473 "source": [
474 "main"
475 ]
476 },
477 {
478 "cell_type": "code",
479 "execution_count": null,
480 "metadata": {},
481 "outputs": [],
482 "source": []
483 }
484 ],
485 "metadata": {
486 "kernelspec": {
487 "display_name": "Haskell",
488 "language": "haskell",
489 "name": "haskell"
490 },
491 "language_info": {
492 "codemirror_mode": "ihaskell",
493 "file_extension": ".hs",
494 "name": "haskell",
495 "version": "8.0.2"
496 }
497 },
498 "nbformat": 4,
499 "nbformat_minor": 2
500 }