I used brute force.  This article is relevant, though I didn't use it.  http://www.jsoftware.com/papers/play132.htm

MY_INPUT=:    368078

Dyad=: [: :
While=: 2 :'u^:(0-.@:-:v)^:_'
rotate=: |:@:|.

NB. f rotates the current matrix and then prepends the next group of numbers
f=: (,~ (([: i. [: - _1 { $)+[:>:(<0 _1)&{))@:rotate

NB. example use of f        f^:14 ,:,1
NB.                         f While(MY_INPUT > (<0 0)&{),:,1


NB. computes the multi-dimensional index vector of x in y
find_index=: ([: , $@:] #: I.@:(= ,)) Dyad

A=:f While(MY_INPUT > (<0 0)&{),:,1

   1 find_index A
303 303
   MY_INPUT find_index A
0 371
   +/ | 0 371 - 303 303
371


NB. part2 looks up the index of the next position

NB. using the spiral matrix from the first part.

NB. Then it sums up the neighborhood in a matrix

NB. of same size with 1 in the center and all other

NB. zeros, and modifies that matrix.

part2=: 4 :0
 a=. 1 = y
 i=. 1
 whilst. s < x do.
  k =. < j =. i find_index y
  s =. j (+/@:,@:([;.0~ (3 ,:~ _1 + [))~) a
  a =. j (s"_)`(k"_)`]} a
  i =. >: i
 end.
 a
)


  C=:MY_INPUT part2 A
  >./,C

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

Reply via email to