Day 3
[advent-of-code-17.git] / src / advent03 / advent03.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE FlexibleContexts #-}\n",
10 "{-# LANGUAGE MultiWayIf #-}"
11 ]
12 },
13 {
14 "cell_type": "code",
15 "execution_count": 2,
16 "metadata": {},
17 "outputs": [],
18 "source": [
19 "import Data.List (tails)\n",
20 "import qualified Data.HashMap.Strict as M\n",
21 "import Data.HashMap.Strict ((!))\n",
22 "import Control.Monad.State.Lazy"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 3,
28 "metadata": {},
29 "outputs": [],
30 "source": [
31 "offsets2 = repeat 8"
32 ]
33 },
34 {
35 "cell_type": "code",
36 "execution_count": 4,
37 "metadata": {},
38 "outputs": [
39 {
40 "data": {
41 "text/plain": [
42 "[8,8,8]"
43 ]
44 },
45 "metadata": {},
46 "output_type": "display_data"
47 }
48 ],
49 "source": [
50 "take 3 offsets2"
51 ]
52 },
53 {
54 "cell_type": "code",
55 "execution_count": 5,
56 "metadata": {},
57 "outputs": [
58 {
59 "data": {
60 "text/plain": [
61 "[8,16,24]"
62 ]
63 },
64 "metadata": {},
65 "output_type": "display_data"
66 }
67 ],
68 "source": [
69 "take 3 $ scanl (+) 8 offsets2"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 6,
75 "metadata": {},
76 "outputs": [],
77 "source": [
78 "diagonal n = scanl (+) 1 $ scanl (+) n $ repeat 8"
79 ]
80 },
81 {
82 "cell_type": "code",
83 "execution_count": 7,
84 "metadata": {},
85 "outputs": [
86 {
87 "data": {
88 "text/plain": [
89 "[1,9,25,49,81]"
90 ]
91 },
92 "metadata": {},
93 "output_type": "display_data"
94 }
95 ],
96 "source": [
97 "dr = diagonal 8\n",
98 "take 5 dr"
99 ]
100 },
101 {
102 "cell_type": "code",
103 "execution_count": 8,
104 "metadata": {},
105 "outputs": [
106 {
107 "data": {
108 "text/plain": [
109 "[1,5,17,37,65]"
110 ]
111 },
112 "metadata": {},
113 "output_type": "display_data"
114 }
115 ],
116 "source": [
117 "ul = diagonal 4\n",
118 "take 5 ul"
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": 9,
124 "metadata": {},
125 "outputs": [
126 {
127 "data": {
128 "text/plain": [
129 "[1,3,13,31,57]"
130 ]
131 },
132 "metadata": {},
133 "output_type": "display_data"
134 }
135 ],
136 "source": [
137 "ur = diagonal 2\n",
138 "take 5 ur"
139 ]
140 },
141 {
142 "cell_type": "code",
143 "execution_count": 10,
144 "metadata": {},
145 "outputs": [
146 {
147 "data": {
148 "text/plain": [
149 "[1,7,21,43,73]"
150 ]
151 },
152 "metadata": {},
153 "output_type": "display_data"
154 }
155 ],
156 "source": [
157 "dl = diagonal 6\n",
158 "take 5 dl"
159 ]
160 },
161 {
162 "cell_type": "code",
163 "execution_count": 11,
164 "metadata": {},
165 "outputs": [
166 {
167 "data": {
168 "text/plain": [
169 "73"
170 ]
171 },
172 "metadata": {},
173 "output_type": "display_data"
174 }
175 ],
176 "source": [
177 "dl!!4"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": 12,
183 "metadata": {},
184 "outputs": [
185 {
186 "data": {
187 "text/plain": [
188 "(4,73)"
189 ]
190 },
191 "metadata": {},
192 "output_type": "display_data"
193 }
194 ],
195 "source": [
196 "head $ dropWhile ((< 70) . snd) $ zip [0..] dl"
197 ]
198 },
199 {
200 "cell_type": "code",
201 "execution_count": 13,
202 "metadata": {},
203 "outputs": [
204 {
205 "data": {
206 "text/plain": [
207 "(4,81)"
208 ]
209 },
210 "metadata": {},
211 "output_type": "display_data"
212 }
213 ],
214 "source": [
215 "head $ dropWhile ((< 70) . snd) $ zip [0..] dr"
216 ]
217 },
218 {
219 "cell_type": "code",
220 "execution_count": 14,
221 "metadata": {},
222 "outputs": [
223 {
224 "data": {
225 "text/plain": [
226 "[0,1,2]"
227 ]
228 },
229 "metadata": {},
230 "output_type": "display_data"
231 }
232 ],
233 "source": [
234 "take 3 [0..]"
235 ]
236 },
237 {
238 "cell_type": "code",
239 "execution_count": 15,
240 "metadata": {},
241 "outputs": [],
242 "source": [
243 "interleave4 (p:ps) (q:qs) (r:rs) (s:ss) = p:q:r:s:interleave4 ps qs rs ss"
244 ]
245 },
246 {
247 "cell_type": "code",
248 "execution_count": 16,
249 "metadata": {},
250 "outputs": [],
251 "source": [
252 "interleave ([]:_) = []\n",
253 "interleave xss = map head xss ++ interleave (map tail xss)"
254 ]
255 },
256 {
257 "cell_type": "code",
258 "execution_count": 17,
259 "metadata": {},
260 "outputs": [
261 {
262 "data": {
263 "text/plain": [
264 "[(0,1),(1,3),(1,5),(1,7),(1,9),(2,13),(2,17),(2,21),(2,25),(3,31),(3,37),(3,43),(3,49),(4,57),(4,65),(4,73),(4,81),(5,91),(5,101),(5,111)]"
265 ]
266 },
267 "metadata": {},
268 "output_type": "display_data"
269 }
270 ],
271 "source": [
272 "take 20 $ drop 3 $ interleave4 (zip [0..] ur) (zip [0..] ul) (zip [0..] dl) (zip [0..] dr)"
273 ]
274 },
275 {
276 "cell_type": "code",
277 "execution_count": 18,
278 "metadata": {},
279 "outputs": [
280 {
281 "data": {
282 "text/plain": [
283 "[1,1,1,1]"
284 ]
285 },
286 "metadata": {},
287 "output_type": "display_data"
288 }
289 ],
290 "source": [
291 "map head [ur, ul, dl, dr]"
292 ]
293 },
294 {
295 "cell_type": "code",
296 "execution_count": 19,
297 "metadata": {},
298 "outputs": [
299 {
300 "data": {
301 "text/plain": [
302 "[[3,13],[5,17],[7,21],[9,25]]"
303 ]
304 },
305 "metadata": {},
306 "output_type": "display_data"
307 }
308 ],
309 "source": [
310 "map (take 2 . tail) [ur, ul, dl, dr]"
311 ]
312 },
313 {
314 "cell_type": "code",
315 "execution_count": 20,
316 "metadata": {},
317 "outputs": [
318 {
319 "data": {
320 "text/html": [
321 "<style>/* Styles used for the Hoogle display in the pager */\n",
322 ".hoogle-doc {\n",
323 "display: block;\n",
324 "padding-bottom: 1.3em;\n",
325 "padding-left: 0.4em;\n",
326 "}\n",
327 ".hoogle-code {\n",
328 "display: block;\n",
329 "font-family: monospace;\n",
330 "white-space: pre;\n",
331 "}\n",
332 ".hoogle-text {\n",
333 "display: block;\n",
334 "}\n",
335 ".hoogle-name {\n",
336 "color: green;\n",
337 "font-weight: bold;\n",
338 "}\n",
339 ".hoogle-head {\n",
340 "font-weight: bold;\n",
341 "}\n",
342 ".hoogle-sub {\n",
343 "display: block;\n",
344 "margin-left: 0.4em;\n",
345 "}\n",
346 ".hoogle-package {\n",
347 "font-weight: bold;\n",
348 "font-style: italic;\n",
349 "}\n",
350 ".hoogle-module {\n",
351 "font-weight: bold;\n",
352 "}\n",
353 ".hoogle-class {\n",
354 "font-weight: bold;\n",
355 "}\n",
356 ".get-type {\n",
357 "color: green;\n",
358 "font-weight: bold;\n",
359 "font-family: monospace;\n",
360 "display: block;\n",
361 "white-space: pre-wrap;\n",
362 "}\n",
363 ".show-type {\n",
364 "color: green;\n",
365 "font-weight: bold;\n",
366 "font-family: monospace;\n",
367 "margin-left: 1em;\n",
368 "}\n",
369 ".mono {\n",
370 "font-family: monospace;\n",
371 "display: block;\n",
372 "}\n",
373 ".err-msg {\n",
374 "color: red;\n",
375 "font-style: italic;\n",
376 "font-family: monospace;\n",
377 "white-space: pre;\n",
378 "display: block;\n",
379 "}\n",
380 "#unshowable {\n",
381 "color: red;\n",
382 "font-weight: bold;\n",
383 "}\n",
384 ".err-msg.in.collapse {\n",
385 "padding-top: 0.7em;\n",
386 "}\n",
387 ".highlight-code {\n",
388 "white-space: pre;\n",
389 "font-family: monospace;\n",
390 "}\n",
391 ".suggestion-warning { \n",
392 "font-weight: bold;\n",
393 "color: rgb(200, 130, 0);\n",
394 "}\n",
395 ".suggestion-error { \n",
396 "font-weight: bold;\n",
397 "color: red;\n",
398 "}\n",
399 ".suggestion-name {\n",
400 "font-weight: bold;\n",
401 "}\n",
402 "</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\">(map head [ur, ul, dl, dr]) : [[10, 11, 12, 13, 14, 15, 16, 17]]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">map head [ur, ul, dl, dr] : [[10, 11, 12, 13, 14, 15, 16, 17]]</div></div>"
403 ],
404 "text/plain": [
405 "Line 1: Redundant bracket\n",
406 "Found:\n",
407 "(map head [ur, ul, dl, dr]) : [[10, 11, 12, 13, 14, 15, 16, 17]]\n",
408 "Why not:\n",
409 "map head [ur, ul, dl, dr] : [[10, 11, 12, 13, 14, 15, 16, 17]]"
410 ]
411 },
412 "metadata": {},
413 "output_type": "display_data"
414 },
415 {
416 "data": {
417 "text/plain": [
418 "[1,1,1,1,10,11,12,13,14,15,16,17]"
419 ]
420 },
421 "metadata": {},
422 "output_type": "display_data"
423 }
424 ],
425 "source": [
426 "concat $ (map head [ur, ul, dl, dr]) : [[10, 11, 12, 13, 14, 15, 16, 17]]"
427 ]
428 },
429 {
430 "cell_type": "code",
431 "execution_count": 21,
432 "metadata": {},
433 "outputs": [
434 {
435 "data": {
436 "text/plain": [
437 "[0,10,20,30,1,11,21,31,2,12,22,32]"
438 ]
439 },
440 "metadata": {},
441 "output_type": "display_data"
442 }
443 ],
444 "source": [
445 "interleave [[0,1,2], [10,11,12], [20, 21, 22], [30, 31, 32]]"
446 ]
447 },
448 {
449 "cell_type": "code",
450 "execution_count": 22,
451 "metadata": {},
452 "outputs": [
453 {
454 "data": {
455 "text/plain": [
456 "[1,1,1,1,3,5,7,9,13,17,21,25,31,37,43,49,57,65,73,81]"
457 ]
458 },
459 "metadata": {},
460 "output_type": "display_data"
461 }
462 ],
463 "source": [
464 "take 20 $ interleave [ur, ul, dl, dr]"
465 ]
466 },
467 {
468 "cell_type": "code",
469 "execution_count": 23,
470 "metadata": {},
471 "outputs": [
472 {
473 "data": {
474 "text/html": [
475 "<style>/* Styles used for the Hoogle display in the pager */\n",
476 ".hoogle-doc {\n",
477 "display: block;\n",
478 "padding-bottom: 1.3em;\n",
479 "padding-left: 0.4em;\n",
480 "}\n",
481 ".hoogle-code {\n",
482 "display: block;\n",
483 "font-family: monospace;\n",
484 "white-space: pre;\n",
485 "}\n",
486 ".hoogle-text {\n",
487 "display: block;\n",
488 "}\n",
489 ".hoogle-name {\n",
490 "color: green;\n",
491 "font-weight: bold;\n",
492 "}\n",
493 ".hoogle-head {\n",
494 "font-weight: bold;\n",
495 "}\n",
496 ".hoogle-sub {\n",
497 "display: block;\n",
498 "margin-left: 0.4em;\n",
499 "}\n",
500 ".hoogle-package {\n",
501 "font-weight: bold;\n",
502 "font-style: italic;\n",
503 "}\n",
504 ".hoogle-module {\n",
505 "font-weight: bold;\n",
506 "}\n",
507 ".hoogle-class {\n",
508 "font-weight: bold;\n",
509 "}\n",
510 ".get-type {\n",
511 "color: green;\n",
512 "font-weight: bold;\n",
513 "font-family: monospace;\n",
514 "display: block;\n",
515 "white-space: pre-wrap;\n",
516 "}\n",
517 ".show-type {\n",
518 "color: green;\n",
519 "font-weight: bold;\n",
520 "font-family: monospace;\n",
521 "margin-left: 1em;\n",
522 "}\n",
523 ".mono {\n",
524 "font-family: monospace;\n",
525 "display: block;\n",
526 "}\n",
527 ".err-msg {\n",
528 "color: red;\n",
529 "font-style: italic;\n",
530 "font-family: monospace;\n",
531 "white-space: pre;\n",
532 "display: block;\n",
533 "}\n",
534 "#unshowable {\n",
535 "color: red;\n",
536 "font-weight: bold;\n",
537 "}\n",
538 ".err-msg.in.collapse {\n",
539 "padding-top: 0.7em;\n",
540 "}\n",
541 ".highlight-code {\n",
542 "white-space: pre;\n",
543 "font-family: monospace;\n",
544 "}\n",
545 ".suggestion-warning { \n",
546 "font-weight: bold;\n",
547 "color: rgb(200, 130, 0);\n",
548 "}\n",
549 ".suggestion-error { \n",
550 "font-weight: bold;\n",
551 "color: red;\n",
552 "}\n",
553 ".suggestion-name {\n",
554 "font-weight: bold;\n",
555 "}\n",
556 "</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
557 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[zip [0 ..] ur, (zip [0 ..] ul), (zip [0 ..] dl), (zip [0 ..] dr)]</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
558 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[(zip [0 ..] ur), zip [0 ..] ul, (zip [0 ..] dl), (zip [0 ..] dr)]</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
559 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[(zip [0 ..] ur), (zip [0 ..] ul), zip [0 ..] dl, (zip [0 ..] dr)]</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
560 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl), zip [0 ..] dr]</div></div>"
561 ],
562 "text/plain": [
563 "Line 1: Redundant bracket\n",
564 "Found:\n",
565 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
566 " (zip [0 ..] dr)]\n",
567 "Why not:\n",
568 "[zip [0 ..] ur, (zip [0 ..] ul), (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
569 "Found:\n",
570 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
571 " (zip [0 ..] dr)]\n",
572 "Why not:\n",
573 "[(zip [0 ..] ur), zip [0 ..] ul, (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
574 "Found:\n",
575 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
576 " (zip [0 ..] dr)]\n",
577 "Why not:\n",
578 "[(zip [0 ..] ur), (zip [0 ..] ul), zip [0 ..] dl, (zip [0 ..] dr)]Line 1: Redundant bracket\n",
579 "Found:\n",
580 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
581 " (zip [0 ..] dr)]\n",
582 "Why not:\n",
583 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl), zip [0 ..] dr]"
584 ]
585 },
586 "metadata": {},
587 "output_type": "display_data"
588 },
589 {
590 "data": {
591 "text/plain": [
592 "[(0,1),(1,3),(1,5),(1,7),(1,9),(2,13),(2,17),(2,21),(2,25),(3,31),(3,37),(3,43),(3,49),(4,57),(4,65),(4,73),(4,81),(5,91),(5,101),(5,111)]"
593 ]
594 },
595 "metadata": {},
596 "output_type": "display_data"
597 }
598 ],
599 "source": [
600 "take 20 $ drop 3 $ interleave [(zip [0..] ur), (zip [0..] ul), (zip [0..] dl), (zip [0..] dr)]"
601 ]
602 },
603 {
604 "cell_type": "code",
605 "execution_count": 24,
606 "metadata": {},
607 "outputs": [],
608 "source": [
609 "target = 30"
610 ]
611 },
612 {
613 "cell_type": "code",
614 "execution_count": 25,
615 "metadata": {},
616 "outputs": [
617 {
618 "data": {
619 "text/html": [
620 "<style>/* Styles used for the Hoogle display in the pager */\n",
621 ".hoogle-doc {\n",
622 "display: block;\n",
623 "padding-bottom: 1.3em;\n",
624 "padding-left: 0.4em;\n",
625 "}\n",
626 ".hoogle-code {\n",
627 "display: block;\n",
628 "font-family: monospace;\n",
629 "white-space: pre;\n",
630 "}\n",
631 ".hoogle-text {\n",
632 "display: block;\n",
633 "}\n",
634 ".hoogle-name {\n",
635 "color: green;\n",
636 "font-weight: bold;\n",
637 "}\n",
638 ".hoogle-head {\n",
639 "font-weight: bold;\n",
640 "}\n",
641 ".hoogle-sub {\n",
642 "display: block;\n",
643 "margin-left: 0.4em;\n",
644 "}\n",
645 ".hoogle-package {\n",
646 "font-weight: bold;\n",
647 "font-style: italic;\n",
648 "}\n",
649 ".hoogle-module {\n",
650 "font-weight: bold;\n",
651 "}\n",
652 ".hoogle-class {\n",
653 "font-weight: bold;\n",
654 "}\n",
655 ".get-type {\n",
656 "color: green;\n",
657 "font-weight: bold;\n",
658 "font-family: monospace;\n",
659 "display: block;\n",
660 "white-space: pre-wrap;\n",
661 "}\n",
662 ".show-type {\n",
663 "color: green;\n",
664 "font-weight: bold;\n",
665 "font-family: monospace;\n",
666 "margin-left: 1em;\n",
667 "}\n",
668 ".mono {\n",
669 "font-family: monospace;\n",
670 "display: block;\n",
671 "}\n",
672 ".err-msg {\n",
673 "color: red;\n",
674 "font-style: italic;\n",
675 "font-family: monospace;\n",
676 "white-space: pre;\n",
677 "display: block;\n",
678 "}\n",
679 "#unshowable {\n",
680 "color: red;\n",
681 "font-weight: bold;\n",
682 "}\n",
683 ".err-msg.in.collapse {\n",
684 "padding-top: 0.7em;\n",
685 "}\n",
686 ".highlight-code {\n",
687 "white-space: pre;\n",
688 "font-family: monospace;\n",
689 "}\n",
690 ".suggestion-warning { \n",
691 "font-weight: bold;\n",
692 "color: rgb(200, 130, 0);\n",
693 "}\n",
694 ".suggestion-error { \n",
695 "font-weight: bold;\n",
696 "color: red;\n",
697 "}\n",
698 ".suggestion-name {\n",
699 "font-weight: bold;\n",
700 "}\n",
701 "</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
702 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[zip [0 ..] ur, (zip [0 ..] ul), (zip [0 ..] dl), (zip [0 ..] dr)]</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
703 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[(zip [0 ..] ur), zip [0 ..] ul, (zip [0 ..] dl), (zip [0 ..] dr)]</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
704 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[(zip [0 ..] ur), (zip [0 ..] ul), zip [0 ..] dl, (zip [0 ..] dr)]</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\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
705 " (zip [0 ..] dr)]</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl), zip [0 ..] dr]</div></div>"
706 ],
707 "text/plain": [
708 "Line 1: Redundant bracket\n",
709 "Found:\n",
710 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
711 " (zip [0 ..] dr)]\n",
712 "Why not:\n",
713 "[zip [0 ..] ur, (zip [0 ..] ul), (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
714 "Found:\n",
715 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
716 " (zip [0 ..] dr)]\n",
717 "Why not:\n",
718 "[(zip [0 ..] ur), zip [0 ..] ul, (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
719 "Found:\n",
720 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
721 " (zip [0 ..] dr)]\n",
722 "Why not:\n",
723 "[(zip [0 ..] ur), (zip [0 ..] ul), zip [0 ..] dl, (zip [0 ..] dr)]Line 1: Redundant bracket\n",
724 "Found:\n",
725 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
726 " (zip [0 ..] dr)]\n",
727 "Why not:\n",
728 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl), zip [0 ..] dr]"
729 ]
730 },
731 "metadata": {},
732 "output_type": "display_data"
733 }
734 ],
735 "source": [
736 "countedDiags = interleave [(zip [0..] ur), (zip [0..] ul), (zip [0..] dl), (zip [0..] dr)]"
737 ]
738 },
739 {
740 "cell_type": "code",
741 "execution_count": 26,
742 "metadata": {},
743 "outputs": [
744 {
745 "data": {
746 "text/html": [
747 "<style>/* Styles used for the Hoogle display in the pager */\n",
748 ".hoogle-doc {\n",
749 "display: block;\n",
750 "padding-bottom: 1.3em;\n",
751 "padding-left: 0.4em;\n",
752 "}\n",
753 ".hoogle-code {\n",
754 "display: block;\n",
755 "font-family: monospace;\n",
756 "white-space: pre;\n",
757 "}\n",
758 ".hoogle-text {\n",
759 "display: block;\n",
760 "}\n",
761 ".hoogle-name {\n",
762 "color: green;\n",
763 "font-weight: bold;\n",
764 "}\n",
765 ".hoogle-head {\n",
766 "font-weight: bold;\n",
767 "}\n",
768 ".hoogle-sub {\n",
769 "display: block;\n",
770 "margin-left: 0.4em;\n",
771 "}\n",
772 ".hoogle-package {\n",
773 "font-weight: bold;\n",
774 "font-style: italic;\n",
775 "}\n",
776 ".hoogle-module {\n",
777 "font-weight: bold;\n",
778 "}\n",
779 ".hoogle-class {\n",
780 "font-weight: bold;\n",
781 "}\n",
782 ".get-type {\n",
783 "color: green;\n",
784 "font-weight: bold;\n",
785 "font-family: monospace;\n",
786 "display: block;\n",
787 "white-space: pre-wrap;\n",
788 "}\n",
789 ".show-type {\n",
790 "color: green;\n",
791 "font-weight: bold;\n",
792 "font-family: monospace;\n",
793 "margin-left: 1em;\n",
794 "}\n",
795 ".mono {\n",
796 "font-family: monospace;\n",
797 "display: block;\n",
798 "}\n",
799 ".err-msg {\n",
800 "color: red;\n",
801 "font-style: italic;\n",
802 "font-family: monospace;\n",
803 "white-space: pre;\n",
804 "display: block;\n",
805 "}\n",
806 "#unshowable {\n",
807 "color: red;\n",
808 "font-weight: bold;\n",
809 "}\n",
810 ".err-msg.in.collapse {\n",
811 "padding-top: 0.7em;\n",
812 "}\n",
813 ".highlight-code {\n",
814 "white-space: pre;\n",
815 "font-family: monospace;\n",
816 "}\n",
817 ".suggestion-warning { \n",
818 "font-weight: bold;\n",
819 "color: rgb(200, 130, 0);\n",
820 "}\n",
821 ".suggestion-error { \n",
822 "font-weight: bold;\n",
823 "color: red;\n",
824 "}\n",
825 ".suggestion-name {\n",
826 "font-weight: bold;\n",
827 "}\n",
828 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Use guards</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">result\n",
829 " = if pcd == ncd then\n",
830 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
831 " else ncd * 2 - (ncv - target)\n",
832 " else\n",
833 " if (target - pcv + 1) < ncv - target then\n",
834 " pcd * 2 - (target - pcv) + 2 else ncd * 2 - (ncv - target)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">result\n",
835 " | pcd == ncd =\n",
836 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
837 " else ncd * 2 - (ncv - target)\n",
838 " | (target - pcv + 1) < ncv - target = pcd * 2 - (target - pcv) + 2\n",
839 " | otherwise = ncd * 2 - (ncv - target)</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\">(result, pcd, ncd, (target - pcv + 2), ncv - target)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">(result, pcd, ncd, target - pcv + 2, ncv - target)</div></div>"
840 ],
841 "text/plain": [
842 "Line 4: Use guards\n",
843 "Found:\n",
844 "result\n",
845 " = if pcd == ncd then\n",
846 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
847 " else ncd * 2 - (ncv - target)\n",
848 " else\n",
849 " if (target - pcv + 1) < ncv - target then\n",
850 " pcd * 2 - (target - pcv) + 2 else ncd * 2 - (ncv - target)\n",
851 "Why not:\n",
852 "result\n",
853 " | pcd == ncd =\n",
854 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
855 " else ncd * 2 - (ncv - target)\n",
856 " | (target - pcv + 1) < ncv - target = pcd * 2 - (target - pcv) + 2\n",
857 " | otherwise = ncd * 2 - (ncv - target)Line 12: Redundant bracket\n",
858 "Found:\n",
859 "(result, pcd, ncd, (target - pcv + 2), ncv - target)\n",
860 "Why not:\n",
861 "(result, pcd, ncd, target - pcv + 2, ncv - target)"
862 ]
863 },
864 "metadata": {},
865 "output_type": "display_data"
866 },
867 {
868 "data": {
869 "text/plain": [
870 "(7,4,4,3,7)"
871 ]
872 },
873 "metadata": {},
874 "output_type": "display_data"
875 }
876 ],
877 "source": [
878 "target = 58\n",
879 "let corners = head $ dropWhile ((< target) . snd . head . tail) $ tails countedDiags\n",
880 " (pcd, pcv) = head corners\n",
881 " (ncd, ncv) = head $ tail corners\n",
882 " result = if pcd == ncd \n",
883 " then if (target - pcv + 2) < ncv - target\n",
884 " then pcd * 2 - (target - pcv)\n",
885 " else ncd * 2 - (ncv - target)\n",
886 " else if (target - pcv + 1) < ncv - target\n",
887 " then pcd * 2 - (target - pcv) + 2\n",
888 " else ncd * 2 - (ncv - target)\n",
889 " \n",
890 " in (result, pcd, ncd, (target - pcv + 2), ncv - target)"
891 ]
892 },
893 {
894 "cell_type": "code",
895 "execution_count": 27,
896 "metadata": {},
897 "outputs": [
898 {
899 "data": {
900 "text/html": [
901 "<style>/* Styles used for the Hoogle display in the pager */\n",
902 ".hoogle-doc {\n",
903 "display: block;\n",
904 "padding-bottom: 1.3em;\n",
905 "padding-left: 0.4em;\n",
906 "}\n",
907 ".hoogle-code {\n",
908 "display: block;\n",
909 "font-family: monospace;\n",
910 "white-space: pre;\n",
911 "}\n",
912 ".hoogle-text {\n",
913 "display: block;\n",
914 "}\n",
915 ".hoogle-name {\n",
916 "color: green;\n",
917 "font-weight: bold;\n",
918 "}\n",
919 ".hoogle-head {\n",
920 "font-weight: bold;\n",
921 "}\n",
922 ".hoogle-sub {\n",
923 "display: block;\n",
924 "margin-left: 0.4em;\n",
925 "}\n",
926 ".hoogle-package {\n",
927 "font-weight: bold;\n",
928 "font-style: italic;\n",
929 "}\n",
930 ".hoogle-module {\n",
931 "font-weight: bold;\n",
932 "}\n",
933 ".hoogle-class {\n",
934 "font-weight: bold;\n",
935 "}\n",
936 ".get-type {\n",
937 "color: green;\n",
938 "font-weight: bold;\n",
939 "font-family: monospace;\n",
940 "display: block;\n",
941 "white-space: pre-wrap;\n",
942 "}\n",
943 ".show-type {\n",
944 "color: green;\n",
945 "font-weight: bold;\n",
946 "font-family: monospace;\n",
947 "margin-left: 1em;\n",
948 "}\n",
949 ".mono {\n",
950 "font-family: monospace;\n",
951 "display: block;\n",
952 "}\n",
953 ".err-msg {\n",
954 "color: red;\n",
955 "font-style: italic;\n",
956 "font-family: monospace;\n",
957 "white-space: pre;\n",
958 "display: block;\n",
959 "}\n",
960 "#unshowable {\n",
961 "color: red;\n",
962 "font-weight: bold;\n",
963 "}\n",
964 ".err-msg.in.collapse {\n",
965 "padding-top: 0.7em;\n",
966 "}\n",
967 ".highlight-code {\n",
968 "white-space: pre;\n",
969 "font-family: monospace;\n",
970 "}\n",
971 ".suggestion-warning { \n",
972 "font-weight: bold;\n",
973 "color: rgb(200, 130, 0);\n",
974 "}\n",
975 ".suggestion-error { \n",
976 "font-weight: bold;\n",
977 "color: red;\n",
978 "}\n",
979 ".suggestion-name {\n",
980 "font-weight: bold;\n",
981 "}\n",
982 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Use guards</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">result\n",
983 " = if pcd == ncd then\n",
984 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
985 " else ncd * 2 - (ncv - target)\n",
986 " else\n",
987 " if (target - pcv + 1) < ncv - target then\n",
988 " pcd * 2 - (target - pcv) + 2 else ncd * 2 - (ncv - target)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">result\n",
989 " | pcd == ncd =\n",
990 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
991 " else ncd * 2 - (ncv - target)\n",
992 " | (target - pcv + 1) < ncv - target = pcd * 2 - (target - pcv) + 2\n",
993 " | otherwise = ncd * 2 - (ncv - target)</div></div>"
994 ],
995 "text/plain": [
996 "Line 4: Use guards\n",
997 "Found:\n",
998 "result\n",
999 " = if pcd == ncd then\n",
1000 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
1001 " else ncd * 2 - (ncv - target)\n",
1002 " else\n",
1003 " if (target - pcv + 1) < ncv - target then\n",
1004 " pcd * 2 - (target - pcv) + 2 else ncd * 2 - (ncv - target)\n",
1005 "Why not:\n",
1006 "result\n",
1007 " | pcd == ncd =\n",
1008 " if (target - pcv + 2) < ncv - target then pcd * 2 - (target - pcv)\n",
1009 " else ncd * 2 - (ncv - target)\n",
1010 " | (target - pcv + 1) < ncv - target = pcd * 2 - (target - pcv) + 2\n",
1011 " | otherwise = ncd * 2 - (ncv - target)"
1012 ]
1013 },
1014 "metadata": {},
1015 "output_type": "display_data"
1016 },
1017 {
1018 "data": {
1019 "text/plain": [
1020 "480"
1021 ]
1022 },
1023 "metadata": {},
1024 "output_type": "display_data"
1025 }
1026 ],
1027 "source": [
1028 "target = 347991\n",
1029 "let corners = head $ dropWhile ((< target) . snd . head . tail) $ tails countedDiags\n",
1030 " (pcd, pcv) = head corners\n",
1031 " (ncd, ncv) = head $ tail corners\n",
1032 " result = if pcd == ncd \n",
1033 " then if (target - pcv + 2) < ncv - target\n",
1034 " then pcd * 2 - (target - pcv)\n",
1035 " else ncd * 2 - (ncv - target)\n",
1036 " else if (target - pcv + 1) < ncv - target\n",
1037 " then pcd * 2 - (target - pcv) + 2\n",
1038 " else ncd * 2 - (ncv - target)\n",
1039 " \n",
1040 " in result"
1041 ]
1042 },
1043 {
1044 "cell_type": "code",
1045 "execution_count": 28,
1046 "metadata": {},
1047 "outputs": [],
1048 "source": [
1049 "plainDiags = map snd countedDiags"
1050 ]
1051 },
1052 {
1053 "cell_type": "code",
1054 "execution_count": 29,
1055 "metadata": {},
1056 "outputs": [
1057 {
1058 "data": {
1059 "text/plain": [
1060 "[1,3,5,7,9,13,17,21,25,31,37,43,49,57,65,73,81,91,101,111]"
1061 ]
1062 },
1063 "metadata": {},
1064 "output_type": "display_data"
1065 }
1066 ],
1067 "source": [
1068 "take 20 $ drop 3 plainDiags"
1069 ]
1070 },
1071 {
1072 "cell_type": "code",
1073 "execution_count": 30,
1074 "metadata": {},
1075 "outputs": [
1076 {
1077 "data": {
1078 "text/plain": [
1079 "[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10]"
1080 ]
1081 },
1082 "metadata": {},
1083 "output_type": "display_data"
1084 }
1085 ],
1086 "source": [
1087 "steps = concat $ zipWith (\\a b -> [a,b]) [1..] [1..]\n",
1088 "take 20 steps"
1089 ]
1090 },
1091 {
1092 "cell_type": "code",
1093 "execution_count": 31,
1094 "metadata": {},
1095 "outputs": [],
1096 "source": [
1097 "up (a, b) = (a, b + 1)\n",
1098 "down (a, b) = (a, b - 1)\n",
1099 "left (a, b) = (a - 1, b)\n",
1100 "right (a, b) = (a + 1, b)\n",
1101 "directions = [right, up, left, down]"
1102 ]
1103 },
1104 {
1105 "cell_type": "code",
1106 "execution_count": 32,
1107 "metadata": {},
1108 "outputs": [
1109 {
1110 "data": {
1111 "text/html": [
1112 "<style>/* Styles used for the Hoogle display in the pager */\n",
1113 ".hoogle-doc {\n",
1114 "display: block;\n",
1115 "padding-bottom: 1.3em;\n",
1116 "padding-left: 0.4em;\n",
1117 "}\n",
1118 ".hoogle-code {\n",
1119 "display: block;\n",
1120 "font-family: monospace;\n",
1121 "white-space: pre;\n",
1122 "}\n",
1123 ".hoogle-text {\n",
1124 "display: block;\n",
1125 "}\n",
1126 ".hoogle-name {\n",
1127 "color: green;\n",
1128 "font-weight: bold;\n",
1129 "}\n",
1130 ".hoogle-head {\n",
1131 "font-weight: bold;\n",
1132 "}\n",
1133 ".hoogle-sub {\n",
1134 "display: block;\n",
1135 "margin-left: 0.4em;\n",
1136 "}\n",
1137 ".hoogle-package {\n",
1138 "font-weight: bold;\n",
1139 "font-style: italic;\n",
1140 "}\n",
1141 ".hoogle-module {\n",
1142 "font-weight: bold;\n",
1143 "}\n",
1144 ".hoogle-class {\n",
1145 "font-weight: bold;\n",
1146 "}\n",
1147 ".get-type {\n",
1148 "color: green;\n",
1149 "font-weight: bold;\n",
1150 "font-family: monospace;\n",
1151 "display: block;\n",
1152 "white-space: pre-wrap;\n",
1153 "}\n",
1154 ".show-type {\n",
1155 "color: green;\n",
1156 "font-weight: bold;\n",
1157 "font-family: monospace;\n",
1158 "margin-left: 1em;\n",
1159 "}\n",
1160 ".mono {\n",
1161 "font-family: monospace;\n",
1162 "display: block;\n",
1163 "}\n",
1164 ".err-msg {\n",
1165 "color: red;\n",
1166 "font-style: italic;\n",
1167 "font-family: monospace;\n",
1168 "white-space: pre;\n",
1169 "display: block;\n",
1170 "}\n",
1171 "#unshowable {\n",
1172 "color: red;\n",
1173 "font-weight: bold;\n",
1174 "}\n",
1175 ".err-msg.in.collapse {\n",
1176 "padding-top: 0.7em;\n",
1177 "}\n",
1178 ".highlight-code {\n",
1179 "white-space: pre;\n",
1180 "font-family: monospace;\n",
1181 "}\n",
1182 ".suggestion-warning { \n",
1183 "font-weight: bold;\n",
1184 "color: rgb(200, 130, 0);\n",
1185 "}\n",
1186 ".suggestion-error { \n",
1187 "font-weight: bold;\n",
1188 "color: red;\n",
1189 "}\n",
1190 ".suggestion-name {\n",
1191 "font-weight: bold;\n",
1192 "}\n",
1193 "</style><span class='err-msg'>&lt;interactive&gt;:1:1: error:<br/> • No instance for (Show ((t10, t0) -&gt; (t10, t0))) arising from a use of ‘print’<br/> (maybe you haven't applied a function to enough arguments?)<br/> • In a stmt of an interactive GHCi command: print it</span>"
1194 ],
1195 "text/plain": [
1196 "<interactive>:1:1: error:\n",
1197 " • No instance for (Show ((t10, t0) -> (t10, t0))) arising from a use of ‘print’\n",
1198 " (maybe you haven't applied a function to enough arguments?)\n",
1199 " • In a stmt of an interactive GHCi command: print it"
1200 ]
1201 },
1202 "metadata": {},
1203 "output_type": "display_data"
1204 }
1205 ],
1206 "source": [
1207 "take 20 $ concat $ zipWith replicate steps (cycle directions)"
1208 ]
1209 },
1210 {
1211 "cell_type": "code",
1212 "execution_count": 33,
1213 "metadata": {},
1214 "outputs": [
1215 {
1216 "data": {
1217 "text/plain": [
1218 "[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10]"
1219 ]
1220 },
1221 "metadata": {},
1222 "output_type": "display_data"
1223 }
1224 ],
1225 "source": [
1226 "take 20 steps"
1227 ]
1228 },
1229 {
1230 "cell_type": "code",
1231 "execution_count": 34,
1232 "metadata": {},
1233 "outputs": [],
1234 "source": [
1235 "locations = scanl (\\c f -> f c) (0,0) $ concat $ zipWith replicate steps (cycle directions)\n",
1236 " where\n",
1237 " steps = concat $ zipWith (\\a b -> [a,b]) [1..] [1..]"
1238 ]
1239 },
1240 {
1241 "cell_type": "code",
1242 "execution_count": 35,
1243 "metadata": {},
1244 "outputs": [
1245 {
1246 "data": {
1247 "text/plain": [
1248 "[(0,0),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1),(2,-1),(2,0),(2,1),(2,2),(1,2),(0,2),(-1,2),(-2,2),(-2,1),(-2,0),(-2,-1)]"
1249 ]
1250 },
1251 "metadata": {},
1252 "output_type": "display_data"
1253 }
1254 ],
1255 "source": [
1256 "take 20 locations"
1257 ]
1258 },
1259 {
1260 "cell_type": "code",
1261 "execution_count": 36,
1262 "metadata": {},
1263 "outputs": [
1264 {
1265 "data": {
1266 "text/plain": [
1267 "fromList [((0,0),1)]"
1268 ]
1269 },
1270 "metadata": {},
1271 "output_type": "display_data"
1272 }
1273 ],
1274 "source": [
1275 "M.singleton (0, 0) 1"
1276 ]
1277 },
1278 {
1279 "cell_type": "code",
1280 "execution_count": 37,
1281 "metadata": {},
1282 "outputs": [
1283 {
1284 "data": {
1285 "text/plain": [
1286 "fromList [((0,0),1),((0,1),4),((1,1),2),((1,0),1),((-1,1),5),((-1,0),10)]"
1287 ]
1288 },
1289 "metadata": {},
1290 "output_type": "display_data"
1291 }
1292 ],
1293 "source": [
1294 "m0 = M.fromList [((0,0), 1),((1,0), 1), ((1,1), 2), ((0,1), 4), ((-1,1), 5), ((-1,0), 10)]\n",
1295 "m0"
1296 ]
1297 },
1298 {
1299 "cell_type": "code",
1300 "execution_count": 38,
1301 "metadata": {},
1302 "outputs": [],
1303 "source": [
1304 "adjacentMap (r, c) = M.filterWithKey adjacent \n",
1305 " where adjacent k _ = abs (fst k - r) <= 1 && abs (snd k - c) <= 1"
1306 ]
1307 },
1308 {
1309 "cell_type": "code",
1310 "execution_count": 39,
1311 "metadata": {},
1312 "outputs": [
1313 {
1314 "data": {
1315 "text/plain": [
1316 "fromList [((0,0),1),((0,1),4),((-1,1),5),((-1,0),10)]"
1317 ]
1318 },
1319 "metadata": {},
1320 "output_type": "display_data"
1321 }
1322 ],
1323 "source": [
1324 "adjacentMap (-1, 1) m0"
1325 ]
1326 },
1327 {
1328 "cell_type": "code",
1329 "execution_count": 40,
1330 "metadata": {},
1331 "outputs": [],
1332 "source": [
1333 "adjacentMapSum here = M.foldr (+) 0 . adjacentMap here"
1334 ]
1335 },
1336 {
1337 "cell_type": "code",
1338 "execution_count": 41,
1339 "metadata": {},
1340 "outputs": [
1341 {
1342 "data": {
1343 "text/plain": [
1344 "20"
1345 ]
1346 },
1347 "metadata": {},
1348 "output_type": "display_data"
1349 }
1350 ],
1351 "source": [
1352 "adjacentMapSum (-1, 1) m0"
1353 ]
1354 },
1355 {
1356 "cell_type": "code",
1357 "execution_count": 42,
1358 "metadata": {},
1359 "outputs": [],
1360 "source": [
1361 "type Location = (Int, Int)\n",
1362 "type Memory = M.HashMap Location Int"
1363 ]
1364 },
1365 {
1366 "cell_type": "code",
1367 "execution_count": 43,
1368 "metadata": {},
1369 "outputs": [],
1370 "source": [
1371 "emptyMemory = M.singleton (0, 0) 1 "
1372 ]
1373 },
1374 {
1375 "cell_type": "code",
1376 "execution_count": 44,
1377 "metadata": {},
1378 "outputs": [],
1379 "source": [
1380 "updateMemoryOnce :: Location -> State Memory Int\n",
1381 "updateMemoryOnce here = \n",
1382 " do m0 <- get\n",
1383 " let total = adjacentMapSum here m0\n",
1384 " put (M.insert here total m0)\n",
1385 " return total"
1386 ]
1387 },
1388 {
1389 "cell_type": "code",
1390 "execution_count": 45,
1391 "metadata": {},
1392 "outputs": [],
1393 "source": [
1394 "updateMemory :: [Location] -> State Memory Int\n",
1395 "updateMemory [] = do return 0\n",
1396 "updateMemory (l:ls) = \n",
1397 " do updateMemoryOnce l\n",
1398 " updateMemory ls"
1399 ]
1400 },
1401 {
1402 "cell_type": "code",
1403 "execution_count": 46,
1404 "metadata": {},
1405 "outputs": [
1406 {
1407 "data": {
1408 "text/plain": [
1409 "(0,fromList [((0,0),1),((5,-1),6573553),((1,3),5336),((-1,-3),37402),((-4,4),369601),((4,-4),2909666),((0,1),4),((5,-2),6262851),((1,2),122),((-1,-4),2292124),((4,-3),48065),((-4,5),24242690),((0,2),133),((2,4),279138),((5,-3),6013560),((-3,5),23510079),((1,1),2),((-1,-1),11),((4,-2),98098),((0,3),5733),((2,5),18565223),((5,-4),2957731),((-3,4),363010),((1,0),1),((-1,-2),747),((4,-1),103128),((0,4),312453),((2,2),59),((-2,-2),362),((-5,5),24612291),((-3,3),6591),((3,-3),47108),((-4,0),875851),((0,5),20390510),((-2,-1),351),((2,3),5022),((-3,2),13486),((3,-4),2814493),((-4,1),830037),((2,0),54),((-2,-4),2179400),((3,-1),1968),((-3,1),14267),((1,5),19452043),((-4,2),787032),((2,1),57),((-2,-3),35487),((3,-2),957),((-3,0),15252),((1,4),295229),((-4,3),752688),((4,4),130654),((-4,-4),1026827),((-1,5),21383723),((3,1),2275),((-3,-1),16295),((-2,2),147),((2,-2),931),((-4,-3),1009457),((4,5),17048404),((-1,4),330785),((3,0),2105),((-3,-2),17008),((2,-1),26),((-2,3),6444),((-4,-2),975079),((3,3),2450),((-3,-3),17370),((5,5),8391037),((-2,0),330),((2,-4),2674100),((-4,-1),924406),((3,2),2391),((-3,-4),2089141),((5,4),8260383),((-2,1),304),((2,-3),45220),((4,0),109476),((-1,1),5),((1,-1),25),((3,5),17724526),((5,3),8001525),((0,-4),2411813),((4,1),116247),((-1,0),10),((1,-2),880),((3,4),266330),((5,2),7619304),((0,-3),39835),((4,2),123363),((-1,3),6155),((1,-3),42452),((5,1),7251490),((-2,4),349975),((0,-2),806),((4,3),128204),((-1,2),142),((1,-4),2539320),((5,0),6902404),((-2,5),22427493),((0,-1),23)])"
1410 ]
1411 },
1412 "metadata": {},
1413 "output_type": "display_data"
1414 }
1415 ],
1416 "source": [
1417 "runState (updateMemory (take 100 $ drop 1 locations)) emptyMemory"
1418 ]
1419 },
1420 {
1421 "cell_type": "code",
1422 "execution_count": 51,
1423 "metadata": {},
1424 "outputs": [],
1425 "source": [
1426 "memoryValues = execState (updateMemory (take 100 $ drop 1 locations)) emptyMemory"
1427 ]
1428 },
1429 {
1430 "cell_type": "code",
1431 "execution_count": 48,
1432 "metadata": {},
1433 "outputs": [
1434 {
1435 "data": {
1436 "text/plain": [
1437 "347991"
1438 ]
1439 },
1440 "metadata": {},
1441 "output_type": "display_data"
1442 }
1443 ],
1444 "source": [
1445 "target"
1446 ]
1447 },
1448 {
1449 "cell_type": "code",
1450 "execution_count": 52,
1451 "metadata": {},
1452 "outputs": [
1453 {
1454 "data": {
1455 "text/plain": [
1456 "(-2,4)"
1457 ]
1458 },
1459 "metadata": {},
1460 "output_type": "display_data"
1461 }
1462 ],
1463 "source": [
1464 "head $ dropWhile (\\l -> memoryValues!l <= target) locations"
1465 ]
1466 },
1467 {
1468 "cell_type": "code",
1469 "execution_count": 53,
1470 "metadata": {},
1471 "outputs": [
1472 {
1473 "data": {
1474 "text/plain": [
1475 "349975"
1476 ]
1477 },
1478 "metadata": {},
1479 "output_type": "display_data"
1480 }
1481 ],
1482 "source": [
1483 "(!) memoryValues $ head $ dropWhile (\\l -> memoryValues!l <= target) locations"
1484 ]
1485 },
1486 {
1487 "cell_type": "code",
1488 "execution_count": null,
1489 "metadata": {},
1490 "outputs": [],
1491 "source": []
1492 }
1493 ],
1494 "metadata": {
1495 "kernelspec": {
1496 "display_name": "Haskell",
1497 "language": "haskell",
1498 "name": "haskell"
1499 },
1500 "language_info": {
1501 "codemirror_mode": "ihaskell",
1502 "file_extension": ".hs",
1503 "name": "haskell",
1504 "version": "8.0.2"
1505 }
1506 },
1507 "nbformat": 4,
1508 "nbformat_minor": 2
1509 }