Roger wrote:
>  n        =: 5
>  Neighbors=: 3 3 ((>:i.n,n) e. (9$_1 1 _1 1 1 1) * ,);._3 >:m
,(m,.(i.n,n),.m),m=: *:n
>  out      =: [ ~: Neighbors {~ <@]

Now that's what I call array-oriented!  Generating the 4-dimensional catalog
of boards, indexed by light.  And I like the use of tessellation (my
original solution also used  ;._3  but not so elegantly).  Wonderful.

In gratitude, I present a different way to calculate Neighbors, which may
reveal something of the game's nature:

           Neighbors =:  #: (0 ,. -<:i.n) |.!.0 / (,~n) {. (,. ] ,.~ -: <.@
* 7 3 {~ # {.1:) 2^i.-n

Note the all the twos (2&^ and -: and #:).  The only wart in the expression
is  ... <.@ * 7 3 {~ # {.1:  where we're mostly multiplying by 7, except at
the ends where we multiply by 3 (and at the trailing end we also do not
halve, hence <.).  I suppose this is this representation's expression of the
edge conditions, as the  *:n  border is in yours. 

Since this wart shows up in every representation, perhaps it is inescapable.
Or maybe it is a part of the definition of the game.  There are some
versions where the board is toroidal, i.e. the top "wraps around" to the
bottom and so does the left to the right, so there are no edges or corners
(and so no edge or corner cases).

-Dan

PS:  The multi-axis features of  |.  and  {.  is what makes this expression
sweet.  In Raul's solution, if we implemented the feature request at [1], he
could have similarly sweetened his expression.

[1]
http://www.jsoftware.com/jwiki/System/Interpreter/Requests#redefineu.3B.0y 

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

Reply via email to