NB. puzzle: substitute the digits 0-9 for the the letters
   NB.         to form a magic square of sum 99

    NB. (published puzzle hinted that y is 8 should you try it with pen and paper)

   _4 [\ ;:'ph h ne th na tc he e pn nx y pp a pt pv nc'
┌──┬──┬──┬──┐
│ph│h │ne│th│
├──┼──┼──┼──┤
│na│tc│he│e │
├──┼──┼──┼──┤
│pn│nx│y │pp│
├──┼──┼──┼──┤
│a │pt│pv│nc│
└──┴──┴──┴──┘


   NB. a solution with Engine: j902/j64avx2/linux  Beta-j: commercial/2020-10-29T16:04:59
   NB. !10 is small enough to try all permutations.

   NB. method: select solutions from all literal puzzle substitutions

   A=:'ph h ne th na tc he e pn nx y pp a pt pv nc'
   S=: ' ' ([ ,~ (-.~ ~.)) A  NB. 'phnetacxyv '  the set of puzzle character with space at index 10

   NB. I is the puzzle converted to indices,
   I=: S i. A

   NB. w computes to 1 iff all rows,  columns, and diagonals sum to 99
   w=: (0 -.@e. 99 = +/ , +/"1 , +/@:((<0 1)&|:) , +/@:((<0 1)&|:)@:|:)@:(_4 [\ 0&".)

   NB. P is a table of all permutations of the digits, followed by the space character
   P=:' ' ,.~ ({~ (A.&i.~ !)@:#) Num_j_

   NB. literal table J are all substitutions
   J=:I {"1 P
   NB. for example here are the head and tail rows
   ({. ,: {:) J
01 1 23 41 25 46 13 3 02 27 8 00 5 04 09 26
98 8 76 58 74 53 86 6 97 72 1 99 4 95 90 73

   NB. filter solutions
   SOLUTIONS=: (#~ w"1) J

   $ SOLUTIONS
1 43

   datatype SOLUTIONS
literal

   A, SOLUTIONS
ph h ne th na tc he e pn nx y pp a pt pv nc
21 1 46 31 47 30 16 6 24 45 8 22 7 23 29 40

   (_4 ]\ ".)"1 SOLUTIONS
21  1 46 31
47 30 16  6
24 45  8 22
 7 23 29 40

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to