Essays/Krypto
Krypto 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.
deck =: (3#1+i.6),(4#7+i.4),(2#11+i.7),18+i.8
Six cards are dealt: an objective card and five other cards. A player must use all five of the latter cards' numbers exactly once, using any combination of arithmetic operations (+ , - , * , and %) to form the objective card's number. The first player to come up with a correct formula is the winner. The more strict "International Rules" specify the use of positive integers only; fractional and negative intermediate results are not permitted.
A Solution
There are a maximum of !5 permutations of the 5 cards and 4 possibilities in each of the 4 places where an operation can be put, for (!5)*4^4 or 30720 total possibilities. This number is small enough to be amenable to an exhaustive approach.
deal =: deck {~ 6 ? (#deck)"_ allexp =: ((256*!5)$'+-*%'{~(4$4)#:i.256) (<a:;6+5*i.4)} 256#5":(i.!5) A. ] solve =: {. ([ ; (=".) ~.@# ]) allexp@}. Krypto =: solve @ deal t=: Krypto 0 t ┌──┬─────────────────────────┐ │17│ 8 - 19 + 14 - 2 * 21│ │ │ 8 - 19 + 14 - 21 * 2│ │ │ 8 - 19 - 21 + 14 % 2│ │ │ 8 - 14 + 19 - 2 * 21│ │ │ 8 - 14 + 19 - 21 * 2│ │ │ 8 + 2 - 14 % 19 - 21│ │ │ ... │ │ │ 21 + 8 - 19 - 14 % 2│ │ │ 21 - 19 - 8 + 14 % 2│ └──┴─────────────────────────┘ $ t 2 $&.> t ┌┬─────┐ ││24 25│ └┴─────┘
Intermediate Steps
] d=: deal 0 17 8 19 14 2 21 a=: allexp }.d $a 30720 25 8{.a 8 + 19 + 14 + 2 + 21 8 + 19 + 14 + 2 - 21 8 + 19 + 14 + 2 * 21 8 + 19 + 14 + 2 % 21 8 + 19 + 14 - 2 + 21 8 + 19 + 14 - 2 - 21 8 + 19 + 14 - 2 * 21 8 + 19 + 14 - 2 % 21 _5{.a 21 % 2 % 14 * 19 % 8 21 % 2 % 14 % 19 + 8 21 % 2 % 14 % 19 - 8 21 % 2 % 14 % 19 * 8 21 % 2 % 14 % 19 % 8 +/ 17 = ". a 24 solve d ┌──┬─────────────────────────┐ │17│ 8 - 19 + 14 - 2 * 21│ │ │ 8 - 19 + 14 - 21 * 2│ │ │ 8 - 19 - 21 + 14 % 2│ │ │ 8 - 14 + 19 - 2 * 21│ │ │ 8 - 14 + 19 - 21 * 2│ │ │ 8 + 2 - 14 % 19 - 21│ │ │ ... │ │ │ 21 + 8 - 19 - 14 % 2│ │ │ 21 - 19 - 8 + 14 % 2│ └──┴─────────────────────────┘
deal 0 deals 6 cards from the deck. The first is treated as the objective card.
allexp c takes a 5-element integer list argument and produces a 30720-row character table result, consisting of all !5 permutations of the 5 cards interspersed with all 4^4 arrangements of the four operations + - * % .
Executing the rows of the result a of allexp produces a 30720 numeric list. Comparison of this list with the objective card results in a boolean list that selects the rows of a that are correct formulas.
International Rules
The international rules can be applied readily:
interm =: _7 _12 _17 ".@{."0 1"1 (0 8&}.) posint =: [: *./"1 (=<.) *. 0&<: irules =: (; ] #~ posint@interm)&>/ $&.> irules t ┌┬────┐ ││8 25│ └┴────┘ irules t ┌──┬─────────────────────────┐ │17│ 8 + 2 + 14 % 21 - 19│ │ │ 8 + 21 - 19 - 14 % 2│ │ │ 19 - 2 * 8 - 21 - 14│ │ │ 19 - 2 % 8 - 21 - 14│ │ │ 19 - 2 * 14 - 21 - 8│ │ │ 19 - 2 % 14 - 21 - 8│ │ │ 2 + 8 + 14 % 21 - 19│ │ │ 21 - 19 - 8 + 14 % 2│ └──┴─────────────────────────┘
Collected Definitions
deck =: (3#1+i.6),(4#7+i.4),(2#11+i.7),18+i.8 deal =: deck {~ 6 ? (#deck)"_ allexp =: ((256*!5)$'+-*%'{~(4$4)#:i.256) (<a:;6+5*i.4)} 256#5":(i.!5) A. ] solve =: {. ([ ; (=".) ~.@# ]) allexp@}. Krypto =: solve @ deal interm =: _7 _12 _17 ".@{."0 1"1 (0 8&}.) posint =: [: *./"1 (=<.) *. 0&<: irules =: (; ] #~ posint@interm)&>/
See also
Contributed by Roger Hui.