Abelian sandpile model: Difference between revisions
simpler Forth solution
m (small Forth improvement) |
(simpler Forth solution) |
||
Line 32:
{{works with|gforth|0.7.3}}
<br>
<lang forth>#! /usr/bin/gforth -
\ Abelian Sandpile Model
Line 52:
size^2 allot-erase arr
\ array processing
Line 64 ⟶ 62:
\ stack processing
:
: stack-full? stack-empty? invert ;
\ pgm-handling
Line 84 ⟶ 75:
\ sandpile
: prep-arr HEIGHT center ix ! ;
: prep-stack -1 HEIGHT 4 u>= if center
: prepare prep-arr prep-stack ;
: ensure if else 2drop 0 2rdrop exit then ;
Line 98 ⟶ 88:
: south 1. d+ ;
: west 1- ;
: reduce
: increase 2dup legal? if 2dup ix dup 1 swap +! @ 4 = if
: inc-north 2dup north increase ;
: inc-east 2dup east increase ;
Line 105 ⟶ 95:
: inc-west 2dup west increase ;
: inc-all inc-north inc-east inc-south inc-west 2drop ;
: simulate prepare begin stack-full? while 2dup 2>r reduce 2r> inc-all repeat drop to-pgm ." written to " filename type cr ;
simulate bye</lang>
|