Point 24 Continued
Krypto

by Zhe Hu

Given six numbers drawn from a set of cards ranging from 1 to 25, the sixth one is the objective, which the other five cards try to reach through arithmetic operations  \(TraditionalForm\`\((\(+\(\(,\)\(\ \)\(-\ ×\)\(,\)\(\ \)\(÷\)\)\))\)\).

Background

After the previous submission about "Point 24". I got advice from Ed Pegg Jr. to expand the code to other card games, like Krypto.

I learnt about Krypto from Marcus Kazmierczak's webpage  http://www.mkaz.com/math/krypto/

In general, computer solutions of combinatorial card games like these involves three key steps: permutation of the card numbers, permutation of the operators, groupings of the operators and the operands.

Because the operators like + and × are associative, groupings like ( (A B) C ) is equivalent to (C (A B)). However, the operators like ÷ and - are not associative.

The good thing about "Point 24" is that four cards number from 1 to 13 that are all smaller than 24. So, for example, (C (A B)) can be left out. In the end the grouping rules are simplified to two possibilities, i.e. x( y ( z (A, B), C), D) and x( y (A, B), z (C, D)) , where A, B, C, D are in the set {1,  ... , 13} and x, y, z are in the set \(TraditionalForm\`{\(×\)\(\(,\)\(\ \)\(\(÷\)\(\(,\)\(\ \)\(+\(\(,\)\(\ \)\(-\)\)\)\)\)\)}\).

This no longer holds true for Krypto in which the objective is also a card from 1 to 25 so that it can be a number smaller than the other cards. However we can use Will Self's trick to make subtraction and division also associative by defining reverse subtraction and reverse division.

subtractionReverse[x_?NumberQ,  y_?NumberQ] := y - x

divisionReverse[x_?NumberQ,  y_?NumberQ] := 
      If[x == 0,  Null,  y/x]

In this notebook,  we use the symbol "r-r" to represent reverse subtraction: a (r - 
              r) b  =  b -a and symbol "r%r" to represent reverse division: a (r  %  r) b  =  b ÷a

Modified Code for Krypto

Now we have five card numbers
A u B v C w D x E
where A, B, C, D, E are in the set {1,  ... , 25} and u, v, w, x are in the set \(TraditionalForm\`{\(×\)\(\(,\)\(\ \)\(\(÷\)\(\(,\)\(\ \)\(+\(\(,\)\(\ \)\(-\)\)\)\)\)\)}\)
Still use Mathematica to do permutations of five numbers, the groupings with operators become 
(1)  u ( v ( w ( x (A, B), C), D), E)
(2)  x ( u ( v (A, B), w (C, D), E) 
(3) x ( u (A, B), v ( w (C, D), E))

Remove[f1, f2, f3,  divide, printf1, printf2, krypto]

f1[operations[u_, v_, w_, x_],  numbers[a_,  b_,  c_,  d_,  e_]] := 
      u[v[w[x[a, b], c], d], e]

f2[operations[u_, v_, w_, x_],  
          numbers[a_,  b_,  c_,  d_,  e_]] := x[u[v[a, b], w[c, d]], e]

f3[operations[u_, v_, w_, x_], numbers[a_, b_, c_, d_, e_]] := 
      x[u[a, b], v[w[c, d], e]]

divide[x_?NumberQ, y_?NumberQ] = 
        If[y == 0, Null, x/y];

\(printf1[operations[u_, v_, w_, x_], numbers[a_, b_, c_, d_, e_]] := 
      StringReplace["(" <> "(" <> "(" <> ToString[a] <> 
          ToString[x] <> ToString[b] <> ")" <> ToString[w] <> 
          ToString[c] <> ")" <> ToString[v] <> ToString[d] <> ")" <> 
          ToString[u] <> 
          ToString[
            e], {"Plus" -> "+", \ "Subtract" -> "-", \ "Times" -> "×", \ "divide" -> "÷", "subtractionReverse" -> " r-r ", "divisionReverse" -> " r%r "}]\)

\(printf2[operations[u_, v_, w_, x_], numbers[a_, b_, c_, d_, e_]] := 
      StringReplace["(" <> "(" <> ToString[a] <> ToString[v] <> 
          ToString[b] <> ")" <> ToString[u] <> "(" <> ToString[c] <> 
          ToString[w] <> ToString[d] <> ")" <> ")" <> ToString[x] <> 
          ToString[
            e], {"Plus" -> "+", \ "Subtract" -> "-", \ "Times" -> "×", \ "divide" -> "÷", "subtractionReverse" -> " r-r ", "divisionReverse" -> " r%r "}]\)

\(printf3[operations[u_, v_, w_, x_], numbers[a_, b_, c_, d_, e_]] := 
      StringReplace["(" <> ToString[a] <> ToString[u] <> 
          ToString[b] <> ")" <> ToString[x] <> "(" <> "(" <> 
          ToString[c] <> ToString[w] <> ToString[d] <> ")" <> 
          ToString[v] <> 
          ToString[
            e] <> ")", {"Plus" -> "+", \ "Subtract" -> "-", \ "Times" -> "×", \ "divide" -> "÷", "subtractionReverse" -> " r-r ", "divisionReverse" -> " r%r "}]\)

krypto[{a_, b_, c_, d_, e_, f_}] := 
      Module[{temp}, 
temp = 
          Flatten[ 
            Outer[use,  
              Flatten[Outer[operations,  
                  Sequence  @@  
                    Table[{Plus,  Subtract,  Times,  divide, 
                        subtractionReverse,  
                        divisionReverse},  {4}]]],  numbers  @@  #  &  /@  Permutations[{a,  b,  c,  d, e}]]]; {Map[# /. use -> printf1 &, 
            Select[temp,  (#  /.  use  ->  f1)  ==  f  &]], 
          Map[# /. use -> printf2 &, 
            Select[temp, (# /. use -> f2) == f &]], 
          Map[# /. use -> printf3 &,  
            Select[temp, (# /. use -> f3) == 
                  f &]]}]

krypto[{19, 19, 7, 7, 7, 18}]

Links of Interest

Dave Rusin's webpage http://www.math.niu.edu/~rusin/uses-math/games/krypto/

Marcus Kazmierczak's webpage http://www.mkaz.com/math/krypto/

About the Author

Zhe Hu is pursuing his Ph. D. degree in biomedical engineering at Illinois Institute of Technology, Chicago, IL
huzhe@iit.edu
http://www.iit.edu/~huzhe