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