9 "{-# LANGUAGE FlexibleContexts #-}\n",
10 "{-# LANGUAGE MultiWayIf #-}"
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"
46 "output_type": "display_data"
65 "output_type": "display_data"
69 "take 3 $ scanl (+) 8 offsets2"
78 "diagonal n = scanl (+) 1 $ scanl (+) n $ repeat 8"
93 "output_type": "display_data"
103 "execution_count": 8,
113 "output_type": "display_data"
123 "execution_count": 9,
133 "output_type": "display_data"
143 "execution_count": 10,
153 "output_type": "display_data"
163 "execution_count": 11,
173 "output_type": "display_data"
182 "execution_count": 12,
192 "output_type": "display_data"
196 "head $ dropWhile ((< 70) . snd) $ zip [0..] dl"
201 "execution_count": 13,
211 "output_type": "display_data"
215 "head $ dropWhile ((< 70) . snd) $ zip [0..] dr"
220 "execution_count": 14,
230 "output_type": "display_data"
239 "execution_count": 15,
243 "interleave4 (p:ps) (q:qs) (r:rs) (s:ss) = p:q:r:s:interleave4 ps qs rs ss"
248 "execution_count": 16,
252 "interleave ([]:_) = []\n",
253 "interleave xss = map head xss ++ interleave (map tail xss)"
258 "execution_count": 17,
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)]"
268 "output_type": "display_data"
272 "take 20 $ drop 3 $ interleave4 (zip [0..] ur) (zip [0..] ul) (zip [0..] dl) (zip [0..] dr)"
277 "execution_count": 18,
287 "output_type": "display_data"
291 "map head [ur, ul, dl, dr]"
296 "execution_count": 19,
302 "[[3,13],[5,17],[7,21],[9,25]]"
306 "output_type": "display_data"
310 "map (take 2 . tail) [ur, ul, dl, dr]"
315 "execution_count": 20,
321 "<style>/* Styles used for the Hoogle display in the pager */\n",
324 "padding-bottom: 1.3em;\n",
325 "padding-left: 0.4em;\n",
329 "font-family: monospace;\n",
330 "white-space: pre;\n",
337 "font-weight: bold;\n",
340 "font-weight: bold;\n",
344 "margin-left: 0.4em;\n",
346 ".hoogle-package {\n",
347 "font-weight: bold;\n",
348 "font-style: italic;\n",
350 ".hoogle-module {\n",
351 "font-weight: bold;\n",
354 "font-weight: bold;\n",
358 "font-weight: bold;\n",
359 "font-family: monospace;\n",
361 "white-space: pre-wrap;\n",
365 "font-weight: bold;\n",
366 "font-family: monospace;\n",
367 "margin-left: 1em;\n",
370 "font-family: monospace;\n",
375 "font-style: italic;\n",
376 "font-family: monospace;\n",
377 "white-space: pre;\n",
382 "font-weight: bold;\n",
384 ".err-msg.in.collapse {\n",
385 "padding-top: 0.7em;\n",
387 ".highlight-code {\n",
388 "white-space: pre;\n",
389 "font-family: monospace;\n",
391 ".suggestion-warning { \n",
392 "font-weight: bold;\n",
393 "color: rgb(200, 130, 0);\n",
395 ".suggestion-error { \n",
396 "font-weight: bold;\n",
399 ".suggestion-name {\n",
400 "font-weight: bold;\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>"
405 "Line 1: Redundant bracket\n",
407 "(map head [ur, ul, dl, dr]) : [[10, 11, 12, 13, 14, 15, 16, 17]]\n",
409 "map head [ur, ul, dl, dr] : [[10, 11, 12, 13, 14, 15, 16, 17]]"
413 "output_type": "display_data"
418 "[1,1,1,1,10,11,12,13,14,15,16,17]"
422 "output_type": "display_data"
426 "concat $ (map head [ur, ul, dl, dr]) : [[10, 11, 12, 13, 14, 15, 16, 17]]"
431 "execution_count": 21,
437 "[0,10,20,30,1,11,21,31,2,12,22,32]"
441 "output_type": "display_data"
445 "interleave [[0,1,2], [10,11,12], [20, 21, 22], [30, 31, 32]]"
450 "execution_count": 22,
456 "[1,1,1,1,3,5,7,9,13,17,21,25,31,37,43,49,57,65,73,81]"
460 "output_type": "display_data"
464 "take 20 $ interleave [ur, ul, dl, dr]"
469 "execution_count": 23,
475 "<style>/* Styles used for the Hoogle display in the pager */\n",
478 "padding-bottom: 1.3em;\n",
479 "padding-left: 0.4em;\n",
483 "font-family: monospace;\n",
484 "white-space: pre;\n",
491 "font-weight: bold;\n",
494 "font-weight: bold;\n",
498 "margin-left: 0.4em;\n",
500 ".hoogle-package {\n",
501 "font-weight: bold;\n",
502 "font-style: italic;\n",
504 ".hoogle-module {\n",
505 "font-weight: bold;\n",
508 "font-weight: bold;\n",
512 "font-weight: bold;\n",
513 "font-family: monospace;\n",
515 "white-space: pre-wrap;\n",
519 "font-weight: bold;\n",
520 "font-family: monospace;\n",
521 "margin-left: 1em;\n",
524 "font-family: monospace;\n",
529 "font-style: italic;\n",
530 "font-family: monospace;\n",
531 "white-space: pre;\n",
536 "font-weight: bold;\n",
538 ".err-msg.in.collapse {\n",
539 "padding-top: 0.7em;\n",
541 ".highlight-code {\n",
542 "white-space: pre;\n",
543 "font-family: monospace;\n",
545 ".suggestion-warning { \n",
546 "font-weight: bold;\n",
547 "color: rgb(200, 130, 0);\n",
549 ".suggestion-error { \n",
550 "font-weight: bold;\n",
553 ".suggestion-name {\n",
554 "font-weight: bold;\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>"
563 "Line 1: Redundant bracket\n",
565 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
566 " (zip [0 ..] dr)]\n",
568 "[zip [0 ..] ur, (zip [0 ..] ul), (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
570 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
571 " (zip [0 ..] dr)]\n",
573 "[(zip [0 ..] ur), zip [0 ..] ul, (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
575 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
576 " (zip [0 ..] dr)]\n",
578 "[(zip [0 ..] ur), (zip [0 ..] ul), zip [0 ..] dl, (zip [0 ..] dr)]Line 1: Redundant bracket\n",
580 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
581 " (zip [0 ..] dr)]\n",
583 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl), zip [0 ..] dr]"
587 "output_type": "display_data"
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)]"
596 "output_type": "display_data"
600 "take 20 $ drop 3 $ interleave [(zip [0..] ur), (zip [0..] ul), (zip [0..] dl), (zip [0..] dr)]"
605 "execution_count": 24,
614 "execution_count": 25,
620 "<style>/* Styles used for the Hoogle display in the pager */\n",
623 "padding-bottom: 1.3em;\n",
624 "padding-left: 0.4em;\n",
628 "font-family: monospace;\n",
629 "white-space: pre;\n",
636 "font-weight: bold;\n",
639 "font-weight: bold;\n",
643 "margin-left: 0.4em;\n",
645 ".hoogle-package {\n",
646 "font-weight: bold;\n",
647 "font-style: italic;\n",
649 ".hoogle-module {\n",
650 "font-weight: bold;\n",
653 "font-weight: bold;\n",
657 "font-weight: bold;\n",
658 "font-family: monospace;\n",
660 "white-space: pre-wrap;\n",
664 "font-weight: bold;\n",
665 "font-family: monospace;\n",
666 "margin-left: 1em;\n",
669 "font-family: monospace;\n",
674 "font-style: italic;\n",
675 "font-family: monospace;\n",
676 "white-space: pre;\n",
681 "font-weight: bold;\n",
683 ".err-msg.in.collapse {\n",
684 "padding-top: 0.7em;\n",
686 ".highlight-code {\n",
687 "white-space: pre;\n",
688 "font-family: monospace;\n",
690 ".suggestion-warning { \n",
691 "font-weight: bold;\n",
692 "color: rgb(200, 130, 0);\n",
694 ".suggestion-error { \n",
695 "font-weight: bold;\n",
698 ".suggestion-name {\n",
699 "font-weight: bold;\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>"
708 "Line 1: Redundant bracket\n",
710 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
711 " (zip [0 ..] dr)]\n",
713 "[zip [0 ..] ur, (zip [0 ..] ul), (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
715 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
716 " (zip [0 ..] dr)]\n",
718 "[(zip [0 ..] ur), zip [0 ..] ul, (zip [0 ..] dl), (zip [0 ..] dr)]Line 1: Redundant bracket\n",
720 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
721 " (zip [0 ..] dr)]\n",
723 "[(zip [0 ..] ur), (zip [0 ..] ul), zip [0 ..] dl, (zip [0 ..] dr)]Line 1: Redundant bracket\n",
725 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl),\n",
726 " (zip [0 ..] dr)]\n",
728 "[(zip [0 ..] ur), (zip [0 ..] ul), (zip [0 ..] dl), zip [0 ..] dr]"
732 "output_type": "display_data"
736 "countedDiags = interleave [(zip [0..] ur), (zip [0..] ul), (zip [0..] dl), (zip [0..] dr)]"
741 "execution_count": 26,
747 "<style>/* Styles used for the Hoogle display in the pager */\n",
750 "padding-bottom: 1.3em;\n",
751 "padding-left: 0.4em;\n",
755 "font-family: monospace;\n",
756 "white-space: pre;\n",
763 "font-weight: bold;\n",
766 "font-weight: bold;\n",
770 "margin-left: 0.4em;\n",
772 ".hoogle-package {\n",
773 "font-weight: bold;\n",
774 "font-style: italic;\n",
776 ".hoogle-module {\n",
777 "font-weight: bold;\n",
780 "font-weight: bold;\n",
784 "font-weight: bold;\n",
785 "font-family: monospace;\n",
787 "white-space: pre-wrap;\n",
791 "font-weight: bold;\n",
792 "font-family: monospace;\n",
793 "margin-left: 1em;\n",
796 "font-family: monospace;\n",
801 "font-style: italic;\n",
802 "font-family: monospace;\n",
803 "white-space: pre;\n",
808 "font-weight: bold;\n",
810 ".err-msg.in.collapse {\n",
811 "padding-top: 0.7em;\n",
813 ".highlight-code {\n",
814 "white-space: pre;\n",
815 "font-family: monospace;\n",
817 ".suggestion-warning { \n",
818 "font-weight: bold;\n",
819 "color: rgb(200, 130, 0);\n",
821 ".suggestion-error { \n",
822 "font-weight: bold;\n",
825 ".suggestion-name {\n",
826 "font-weight: bold;\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",
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",
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>"
842 "Line 4: Use guards\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",
849 " if (target - pcv + 1) < ncv - target then\n",
850 " pcd * 2 - (target - pcv) + 2 else ncd * 2 - (ncv - target)\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",
859 "(result, pcd, ncd, (target - pcv + 2), ncv - target)\n",
861 "(result, pcd, ncd, target - pcv + 2, ncv - target)"
865 "output_type": "display_data"
874 "output_type": "display_data"
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",
890 " in (result, pcd, ncd, (target - pcv + 2), ncv - target)"
895 "execution_count": 27,
901 "<style>/* Styles used for the Hoogle display in the pager */\n",
904 "padding-bottom: 1.3em;\n",
905 "padding-left: 0.4em;\n",
909 "font-family: monospace;\n",
910 "white-space: pre;\n",
917 "font-weight: bold;\n",
920 "font-weight: bold;\n",
924 "margin-left: 0.4em;\n",
926 ".hoogle-package {\n",
927 "font-weight: bold;\n",
928 "font-style: italic;\n",
930 ".hoogle-module {\n",
931 "font-weight: bold;\n",
934 "font-weight: bold;\n",
938 "font-weight: bold;\n",
939 "font-family: monospace;\n",
941 "white-space: pre-wrap;\n",
945 "font-weight: bold;\n",
946 "font-family: monospace;\n",
947 "margin-left: 1em;\n",
950 "font-family: monospace;\n",
955 "font-style: italic;\n",
956 "font-family: monospace;\n",
957 "white-space: pre;\n",
962 "font-weight: bold;\n",
964 ".err-msg.in.collapse {\n",
965 "padding-top: 0.7em;\n",
967 ".highlight-code {\n",
968 "white-space: pre;\n",
969 "font-family: monospace;\n",
971 ".suggestion-warning { \n",
972 "font-weight: bold;\n",
973 "color: rgb(200, 130, 0);\n",
975 ".suggestion-error { \n",
976 "font-weight: bold;\n",
979 ".suggestion-name {\n",
980 "font-weight: bold;\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",
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",
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>"
996 "Line 4: Use guards\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",
1003 " if (target - pcv + 1) < ncv - target then\n",
1004 " pcd * 2 - (target - pcv) + 2 else ncd * 2 - (ncv - target)\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)"
1015 "output_type": "display_data"
1024 "output_type": "display_data"
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",
1044 "cell_type": "code",
1045 "execution_count": 28,
1049 "plainDiags = map snd countedDiags"
1053 "cell_type": "code",
1054 "execution_count": 29,
1060 "[1,3,5,7,9,13,17,21,25,31,37,43,49,57,65,73,81,91,101,111]"
1064 "output_type": "display_data"
1068 "take 20 $ drop 3 plainDiags"
1072 "cell_type": "code",
1073 "execution_count": 30,
1079 "[1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10]"
1083 "output_type": "display_data"
1087 "steps = concat $ zipWith (\\a b -> [a,b]) [1..] [1..]\n",
1092 "cell_type": "code",
1093 "execution_count": 31,
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]"
1105 "cell_type": "code",
1106 "execution_count": 32,
1112 "<style>/* Styles used for the Hoogle display in the pager */\n",
1114 "display: block;\n",
1115 "padding-bottom: 1.3em;\n",
1116 "padding-left: 0.4em;\n",
1119 "display: block;\n",
1120 "font-family: monospace;\n",
1121 "white-space: pre;\n",
1124 "display: block;\n",
1128 "font-weight: bold;\n",
1131 "font-weight: bold;\n",
1134 "display: block;\n",
1135 "margin-left: 0.4em;\n",
1137 ".hoogle-package {\n",
1138 "font-weight: bold;\n",
1139 "font-style: italic;\n",
1141 ".hoogle-module {\n",
1142 "font-weight: bold;\n",
1144 ".hoogle-class {\n",
1145 "font-weight: bold;\n",
1149 "font-weight: bold;\n",
1150 "font-family: monospace;\n",
1151 "display: block;\n",
1152 "white-space: pre-wrap;\n",
1156 "font-weight: bold;\n",
1157 "font-family: monospace;\n",
1158 "margin-left: 1em;\n",
1161 "font-family: monospace;\n",
1162 "display: block;\n",
1166 "font-style: italic;\n",
1167 "font-family: monospace;\n",
1168 "white-space: pre;\n",
1169 "display: block;\n",
1173 "font-weight: bold;\n",
1175 ".err-msg.in.collapse {\n",
1176 "padding-top: 0.7em;\n",
1178 ".highlight-code {\n",
1179 "white-space: pre;\n",
1180 "font-family: monospace;\n",
1182 ".suggestion-warning { \n",
1183 "font-weight: bold;\n",
1184 "color: rgb(200, 130, 0);\n",
1186 ".suggestion-error { \n",
1187 "font-weight: bold;\n",
1190 ".suggestion-name {\n",
1191 "font-weight: bold;\n",
1193 "</style><span class='err-msg'><interactive>:1:1: error:<br/> • No instance for (Show ((t10, t0) -> (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>"
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"
1203 "output_type": "display_data"
1207 "take 20 $ concat $ zipWith replicate steps (cycle directions)"
1211 "cell_type": "code",
1212 "execution_count": 52,
1218 "\"rullddrrruuulllldddd\""
1222 "output_type": "display_data"
1226 "take 20 $ concat $ zipWith replicate steps $ cycle \"ruld\""
1230 "cell_type": "code",
1231 "execution_count": 34,
1235 "locations = scanl (\\c f -> f c) (0,0) $ concat $ zipWith replicate steps (cycle directions)\n",
1237 " steps = concat $ zipWith (\\a b -> [a,b]) [1..] [1..]"
1241 "cell_type": "code",
1242 "execution_count": 35,
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)]"
1252 "output_type": "display_data"
1260 "cell_type": "code",
1261 "execution_count": 36,
1267 "fromList [((0,0),1)]"
1271 "output_type": "display_data"
1275 "M.singleton (0, 0) 1"
1279 "cell_type": "code",
1280 "execution_count": 37,
1286 "fromList [((0,0),1),((0,1),4),((1,1),2),((1,0),1),((-1,1),5),((-1,0),10)]"
1290 "output_type": "display_data"
1294 "m0 = M.fromList [((0,0), 1),((1,0), 1), ((1,1), 2), ((0,1), 4), ((-1,1), 5), ((-1,0), 10)]\n",
1299 "cell_type": "code",
1300 "execution_count": 38,
1304 "adjacentMap (r, c) = M.filterWithKey adjacent \n",
1305 " where adjacent k _ = abs (fst k - r) <= 1 && abs (snd k - c) <= 1"
1309 "cell_type": "code",
1310 "execution_count": 39,
1316 "fromList [((0,0),1),((0,1),4),((-1,1),5),((-1,0),10)]"
1320 "output_type": "display_data"
1324 "adjacentMap (-1, 1) m0"
1328 "cell_type": "code",
1329 "execution_count": 40,
1333 "adjacentMapSum here = M.foldr (+) 0 . adjacentMap here"
1337 "cell_type": "code",
1338 "execution_count": 41,
1348 "output_type": "display_data"
1352 "adjacentMapSum (-1, 1) m0"
1356 "cell_type": "code",
1357 "execution_count": 42,
1361 "type Location = (Int, Int)\n",
1362 "type Memory = M.HashMap Location Int"
1366 "cell_type": "code",
1367 "execution_count": 43,
1371 "emptyMemory = M.singleton (0, 0) 1 "
1375 "cell_type": "code",
1376 "execution_count": 44,
1380 "updateMemoryOnce :: Location -> State Memory Int\n",
1381 "updateMemoryOnce here = \n",
1383 " let total = adjacentMapSum here m0\n",
1384 " put (M.insert here total m0)\n",
1389 "cell_type": "code",
1390 "execution_count": 45,
1394 "updateMemory :: [Location] -> State Memory Int\n",
1395 "updateMemory [] = do return 0\n",
1396 "updateMemory (l:ls) = \n",
1397 " do updateMemoryOnce l\n",
1402 "cell_type": "code",
1403 "execution_count": 46,
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)])"
1413 "output_type": "display_data"
1417 "runState (updateMemory (take 100 $ drop 1 locations)) emptyMemory"
1421 "cell_type": "code",
1422 "execution_count": 47,
1426 "memoryValues = execState (updateMemory (take 100 $ drop 1 locations)) emptyMemory"
1430 "cell_type": "code",
1431 "execution_count": 48,
1441 "output_type": "display_data"
1449 "cell_type": "code",
1450 "execution_count": 49,
1460 "output_type": "display_data"
1464 "head $ dropWhile (\\l -> memoryValues!l <= target) locations"
1468 "cell_type": "code",
1469 "execution_count": 50,
1479 "output_type": "display_data"
1483 "(!) memoryValues $ head $ dropWhile (\\l -> memoryValues!l <= target) locations"
1487 "cell_type": "code",
1488 "execution_count": null,
1496 "display_name": "Haskell",
1497 "language": "haskell",
1501 "codemirror_mode": "ihaskell",
1502 "file_extension": ".hs",