you can use arthur's sparse.q
to create the same table you have, but marching over the matrix all at the same time should be the fastest by of finding neighbors.
sm:{([]row:where count each i;col:raze i;val:raze x@'i:where each x<>0)}
c:raze "L"=i;l:raze "#"=i;
s:{(count x;count first x)} i
d:{x!s sv flip x}flip s vs til prd s
p:flip key d
m:{{?[c y;y;d flip x+s vs y]}[x]/[d flip x+y]}
n:(1_{x cross x}0 1 -1) m: p
seat:{c*(5>a)&x|0=a:sum 0^x n}
sum seat over l
you can use arthur's sparse.q
to create the same table you have, but marching over the matrix all at the same time should be the fastest by of finding neighbors.
sm:{([]row:where count each i;col:raze i;val:raze x@'i:where each x<>0)}
c:raze "L"=i;l:raze "#"=i;
s:{(count x;count first x)} i
d:{x!s sv flip x}flip s vs til prd s
p:flip key d
m:{{?[c y;y;d flip x+s vs y]}[x]/[d flip x+y]}
n:(1_{x cross x}0 1 -1) m: p
seat:{c*(5>a)&x|0=a:sum 0^x n}
sum seat over l