{"id":4062,"date":"2017-05-23T08:23:54","date_gmt":"2017-05-23T08:23:54","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=4062"},"modified":"2017-05-23T08:36:47","modified_gmt":"2017-05-23T08:36:47","slug":"krypto","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2017\/05\/krypto\/","title":{"rendered":"Krypto"},"content":{"rendered":"<p>In the <a href=\"http:\/\/www.dyalog.com\/2016-year-game.htm\">2016 Year Game<\/a>, the task was to generate the numbers 0 to 100 using APL primitives and the digits 2 0 1 6 in that order. For example,<\/p>\n<pre><code class=\"language-apl\">   20=16\r\n   \u00d72016\r\n   2\u230a016\r\n   2+\u00d7016\r\n   \u2026\r\n<\/code><\/pre>\n<p>This &#8220;puzzle of the year&#8221; brings to mind Krypto, a game I played many years ago while in grade school.<\/p>\n<h2>Krypto<\/h2>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Krypto_%28game%29\" target=\"_p2\">Krypto<\/a> is a mathematical card game. The Krypto deck has 56 cards: 3 each of numbers 1-6, 4 each of the numbers 7-10, 2 each of 11-17, 1 each of 18-25.<\/p>\n<pre><code class=\"language-apl\">   \u2395io\u21900\r\n   DECK \u2190 (3\/1+\u23736),(4\/7+\u23734),(2\/11+\u23737),18+\u23738\r\n<\/code><\/pre>\n<p>Six cards are dealt: an objective card and five other cards. A player must use all five of the latter cards&#8217; numbers exactly once, using any combination of arithmetic operations (<code class=\"language-apl\">+<\/code>, <code class=\"language-apl\">-<\/code>, <code class=\"language-apl\">\u00d7<\/code>, and <code class=\"language-apl\">\u00f7<\/code>) to form the objective card&#8217;s number. The first player to come up with a correct formula is the winner. The stricter &#8220;International Rules&#8221; specify the use of non-negative integers only; fractional or negative intermediate results are not permitted.<\/p>\n<p>For example, if the objective card were <code class=\"language-apl\">17<\/code> and the other cards were <code class=\"language-apl\">2<\/code>, <code class=\"language-apl\">8<\/code>, <code class=\"language-apl\">14<\/code>, <code class=\"language-apl\">19<\/code>, and <code class=\"language-apl\">21<\/code>, then a Krypto solution can be as follows. Without loss of generality, we use APL notation and syntax.<\/p>\n<pre><code class=\"language-apl\">   8 - 19 + 14 - 2 \u00d7 21\r\n<\/code><\/pre>\n<p>In this article we present functions to find all solutions to a Krypto hand.<\/p>\n<h2>A Solution<\/h2>\n<p>There are a maximum of <code class=\"language-apl\">!5<\/code> permutations of the <code class=\"language-apl\">5<\/code> cards and <code class=\"language-apl\">4<\/code> possibilities in each of the <code class=\"language-apl\">4<\/code> places where an operation can be put, for <code class=\"language-apl\">(!5)\u00d74*4<\/code> or <code class=\"language-apl\">30720<\/code> total possibilities. This number is small enough for an exhaustive approach.<\/p>\n<pre><code class=\"language-apl\">deal\u2190{DECK \u2337\u2368\u2282 6?\u2262DECK}\r\n\r\nKrypto\u2190{\r\n  perm\u2190{0=\u2375:1 0\u23740 \u22c4 ,[\u23732](\u22820,1+\u2207 \u00af1+\u2375)\u2337\u23641\u2352\u23641\u2218.=\u2368\u2373\u2375}\r\n  a\u2190256\u233f5 0\u2355\u2375[perm 5]\r\n  a[;6+5\u00d7\u23734]\u2190'+-\u00d7\u00f7'[((256\u00d7!5),4)\u2374\u2349(4\u23744)\u22a4\u2373256]\r\n  \u22a3\u2338 a \u233f\u2368 \u237a = \u234e\u23641 \u22a2a\r\n}\r\n\r\n   deal \u236c\r\n17 8 19 14 2 21\r\n\r\n   \u22a2 t\u2190 17 Krypto 8 19 14 2 21\r\n    8 - 19 + 14 -  2 \u00d7 21\r\n    8 - 19 + 14 - 21 \u00d7  2\r\n    8 - 19 - 21 + 14 \u00f7  2\r\n    8 - 14 + 19 -  2 \u00d7 21\r\n        ...\r\n   21 +  8 - 19 - 14 \u00f7  2\r\n   21 - 19 -  8 + 14 \u00f7  2\r\n\r\n   \u2374 t\r\n24 25\r\n<\/code><\/pre>\n<h2>Intermediate Steps<\/h2>\n<p>The function <code class=\"language-apl\">perm<\/code> is from the Dyalog blog post <a href=\"http:\/\/www.dyalog.com\/blog\/2015\/07\/permutations\/\"><em>Permutations<\/em><\/a>, 2015-07-20. <code class=\"language-apl\">perm n<\/code> generates the sorted table of all permutations of <code class=\"language-apl\">\u2373n<\/code>.<\/p>\n<p>The local variable <code class=\"language-apl\">a<\/code> in <code class=\"language-apl\">Krypto<\/code> is a <code class=\"language-apl\">30720<\/code>-row character matrix computed from the <code class=\"language-apl\">5<\/code> non-objective numbers. It consists of all <code class=\"language-apl\">!5<\/code> permutations of the <code class=\"language-apl\">5<\/code> numbers interspersed with all <code class=\"language-apl\">4*4<\/code> arrangements of the four operations <code class=\"language-apl\">+ - \u00d7 \u00f7<\/code>.<\/p>\n<p>Executing the rows of <code class=\"language-apl\">a<\/code> produces a <code class=\"language-apl\">30720<\/code>-element numeric vector. Comparison of this vector with the objective yields a boolean vector that selects the rows of <code class=\"language-apl\">a<\/code> which are correct formulas.<\/p>\n<pre><code class=\"language-apl\">   \u2374 a\r\n30720 25\r\n\r\n   8\u2191a\r\n    8 + 19 + 14 +  2 + 21\r\n    8 + 19 + 14 +  2 - 21\r\n    8 + 19 + 14 +  2 \u00d7 21\r\n    8 + 19 + 14 +  2 \u00f7 21\r\n    8 + 19 + 14 -  2 + 21\r\n    8 + 19 + 14 -  2 - 21\r\n    8 + 19 + 14 -  2 \u00d7 21\r\n    8 + 19 + 14 -  2 \u00f7 21\r\n   \u00af5\u2191a\r\n   21 \u00f7  2 \u00f7 14 \u00d7 19 \u00f7  8\r\n   21 \u00f7  2 \u00f7 14 \u00f7 19 +  8\r\n   21 \u00f7  2 \u00f7 14 \u00f7 19 -  8\r\n   21 \u00f7  2 \u00f7 14 \u00f7 19 \u00d7  8\r\n   21 \u00f7  2 \u00f7 14 \u00f7 19 \u00f7  8\r\n\r\n   +\/ b \u2190 17 = \u234e\u23641 \u22a2a\r\n24\r\n\r\n   t \u2261 b\u233fa\r\n1\r\n<\/code><\/pre>\n<p>We note that use of the <a href=\"http:\/\/dfns.dyalog.com\/n_at.htm\"><code class=\"language-apl\">@<\/code> operator<\/a>, new to Dyalog version 16.0, obviates the need to name intermediate results for reasons for syntax. Instead, names are only used to break the code down into more comprehensible components.<\/p>\n<pre><code class=\"language-apl\">Krypto1\u2190{\r\n  perm\u2190{0=\u2375:1 0\u23740 \u22c4 ,[\u23732](\u22820,1+\u2207 \u00af1+\u2375)\u2337\u23641\u2352\u23641\u2218.=\u2368\u2373\u2375}\r\n  \u22a3\u2338 \u237a (\u22a2(\u233f\u2368)=\u2218(\u234e\u23641)) '+-\u00d7\u00f7'[((256\u00d7!5),4)\u2374\u2349(4\u23744)\u22a4\u2373256]\u22a3@(6+5\u00d7\u23734)\u23641 \u22a2256\u233f5 0\u2355\u2375[perm 5]\r\n}\r\n\r\nKrypto2\u2190{\r\n  perm\u2190{0=\u2375:1 0\u23740 \u22c4 ,[\u23732](\u22820,1+\u2207 \u00af1+\u2375)\u2337\u23641\u2352\u23641\u2218.=\u2368\u2373\u2375}\r\n  fns  \u2190 '+-\u00d7\u00f7'[((256\u00d7!5),4)\u2374\u2349(4\u23744)\u22a4\u2373256]\r\n  nums \u2190 256\u233f5 0\u2355\u2375[perm 5]\r\n  \u22a3\u2338 \u237a (\u22a2(\u233f\u2368)=\u2218(\u234e\u23641)) fns \u22a3@(6+5\u00d7\u23734)\u23641 \u22a2nums\r\n}\r\n\r\n   17 (Krypto \u2261 Krypto1) 8 19 14 2 21\r\n1\r\n\r\n   17 (Krypto \u2261 Krypto2) 8 19 14 2 21\r\n1\r\n\r\n<\/code><\/pre>\n<h2>International Rules<\/h2>\n<p>The international rules (intermediate results must be non-negative integers) can be enforced readily:<\/p>\n<pre><code class=\"language-apl\">irules\u2190{\u2375\u233f\u2368\u2227\/(i=\u230ai)\u22270\u2264i\u21908 13 18{\u234e\u00a8\u237a\u2193\u00a8\u2282\u2375}\u23641\u22a2\u2375}\r\n\r\n   irules 17 Krypto 8 19 14 2 21\r\n    8 +  2 + 14 \u00f7 21 - 19\r\n    8 + 21 - 19 - 14 \u00f7  2\r\n   19 -  2 \u00d7  8 - 21 - 14\r\n   19 -  2 \u00f7  8 - 21 - 14\r\n   19 -  2 \u00d7 14 - 21 -  8\r\n   19 -  2 \u00f7 14 - 21 -  8\r\n    2 +  8 + 14 \u00f7 21 - 19\r\n   21 - 19 -  8 + 14 \u00f7  2\r\n<\/code><\/pre>\n<p>It is instructive to look at how the local variable <code class=\"language-apl\">i<\/code> in <code class=\"language-apl\">irules<\/code> is formed:<\/p>\n<pre><code class=\"language-apl\">   \u22a2 t\u219017 Krypto 8 19 14 2 21\r\n    8 - 19 + 14 -  2 \u00d7 21\r\n    8 - 19 + 14 - 21 \u00d7  2\r\n    8 - 19 - 21 + 14 \u00f7  2\r\n    8 - 14 + 19 -  2 \u00d7 21\r\n        ...\r\n   21 +  8 - 19 - 14 \u00f7  2\r\n   21 - 19 -  8 + 14 \u00f7  2\r\n\r\n   8 13 18 {\u237a\u2193\u00a8\u2282\u2375}\u23641 \u22a2t\r\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u250219 + 14 -  2 \u00d7 21\u250214 -  2 \u00d7 21\u2502 2 \u00d7 21\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250219 + 14 - 21 \u00d7  2\u250214 - 21 \u00d7  2\u250221 \u00d7  2\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250219 - 21 + 14 \u00f7  2\u250221 + 14 \u00f7  2\u250214 \u00f7  2\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250214 + 19 -  2 \u00d7 21\u250219 -  2 \u00d7 21\u2502 2 \u00d7 21\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n            ...\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u2502 8 - 19 - 14 \u00f7  2\u250219 - 14 \u00f7  2\u250214 \u00f7  2\u2502\r\n\u251c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524\r\n\u250219 -  8 + 14 \u00f7  2\u2502 8 + 14 \u00f7  2\u250214 \u00f7  2\u2502\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n\r\n      \u234e\u00a8 8 13 18 {\u237a\u2193\u00a8\u2282\u2375}\u23641 \u22a2t\r\n\u00af9 \u00af28  42\r\n\u00af9 \u00af28  42\r\n\u00af9  28   7\r\n\u00af9 \u00af23  42\r\n   ...\r\n\u00af4  12   7\r\n 4  15   7\r\n<\/code><\/pre>\n<p>(An earlier version of the text appeared as the Jwiki essay <a href=\"http:\/\/code.jsoftware.com\/wiki\/Essays\/Krypto\"><em>Krypto<\/em><\/a>, 2013-07-04.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the 2016 Year Game, the task was to generate the numbers 0 to 100 using APL primitives and the digits 2 0 1 6 in that order. For example, 20=16 \u00d72016 2\u230a016 2+\u00d7016 \u2026 This &#8220;puzzle of the year&#8221; &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2017\/05\/krypto\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,24],"tags":[],"class_list":["post-4062","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-recreational"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4062","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=4062"}],"version-history":[{"count":9,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4062\/revisions"}],"predecessor-version":[{"id":4907,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/4062\/revisions\/4907"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=4062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=4062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=4062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}