I'd like, please, a concise way to express in j the following algorithm.
Input is a 3D binary grid with a block of 1 bits, for example,
A =. 1(<4 4 4)}(9 9 9){.0 NB. 9 cube, true in center
Repeat 3 times:
rotate A 1 unit up, down, right, left, in, out,
combine the resulting 6 groups of data with "or" producing an array
having same shape as A. Also "or" the cells with A.
replace A with the result of those operations.
store the number of 1 bits in A.
result is the vector of 1 bits in A.
I've done this, which seems correct. I envision a solution in 4
dimensions using the iteration operator but find myself rather clueless
about how to write it.
or =. +.
m =. %&2 n =. 30
a =. (3#n) {. 0
a =. 1(m+&.>0 0 0;0 0 1;0 1 0;0 1 1;0 2 0;0 2 1)}a
b =. 0
b =. b,+/,a
a =. a or( 1|."1 a)or(_1|."1 a)or( 1|."2 a)or(_1|."2 a)or( 1|."3
a)or(_1|."3 a)
b =. b,+/,a
a =. a or( 1|."1 a)or(_1|."1 a)or( 1|."2 a)or(_1|."2 a)or( 1|."3
a)or(_1|."3 a)
b =. b,+/,a
a =. a or( 1|."1 a)or(_1|."1 a)or( 1|."2 a)or(_1|."2 a)or( 1|."3
a)or(_1|."3 a)
b =. b,+/,a
a =. a or( 1|."1 a)or(_1|."1 a)or( 1|."2 a)or(_1|."2 a)or( 1|."3
a)or(_1|."3 a)
b =. b,+/,a
a =. a or( 1|."1 a)or(_1|."1 a)or( 1|."2 a)or(_1|."2 a)or( 1|."3
a)or(_1|."3 a)
b =. b,+/,a
1}.6{.b-~1|.b NB. size of enclosing shells, ProjectEuler, #126
Thank you, Dave.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm