game-of-life: count-neighbors needs circular wraparound logic back.

windows-high-dpi
John Benediktsson 2018-03-09 16:53:05 -08:00
parent 5e7bfd7cc4
commit 7179394ea0
1 changed files with 8 additions and 9 deletions

View File

@ -16,23 +16,22 @@ IN: game-of-life
: grid-dim ( grid -- rows cols )
[ length ] [ first length ] bi ;
:: wraparound ( x min max -- y )
x min < [ max ] [ x max > min x ? ] if ; inline
:: count-neighbors ( grid -- counts )
grid grid-dim { fixnum fixnum } declare :> ( rows cols )
rows <iota> [| j |
cols <iota> [| i |
{ -1 0 1 } [
{ -1 0 1 } [
[ i fixnum+fast ] [ j fixnum+fast ] bi*
{ fixnum fixnum } declare :> ( col row )
{
[ col i = row j = and not ]
[ col 0 >= ] [ col cols < ]
[ row 0 >= ] [ row rows < ]
} 0&& [
col row grid
2dup [ zero? ] both? [ 2drop f ] [
[ i fixnum+fast 0 cols 1 - wraparound ]
[ j fixnum+fast 0 rows 1 - wraparound ] bi*
{ fixnum fixnum } declare grid
{ array } declare nth-unsafe
{ bit-array } declare nth-unsafe
] [ f ] if
] if
] with count
] map-sum
] map