omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / drafts / rosettacode / abelian-sandpile-model.wiki
| 1 | =={{header|Omegese}}== |
| 2 | {{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}} |
| 3 | Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program. |
| 4 | <syntaxhighlight lang="text"> |
| 5 | om2 |
| 6 | text nl "\n" |
| 7 | text sp " " |
| 8 | text minus "-" |
| 9 | text ttop "topplings: " |
| 10 | global grid ulong 81 |
| 11 | global tops ulong 1 |
| 12 | note emit writes every byte of a to stdout; a short write is retried, an error exits 1 |
| 13 | fn emit a view uchar * to void |
| 14 | o = ulong 0 |
| 15 | while lt o len a bound 4096 |
| 16 | k = write 1 a o sub len a o |
| 17 | if le k 0 |
| 18 | exit 1 |
| 19 | end |
| 20 | o = add o ulong k |
| 21 | end |
| 22 | end |
| 23 | note emitu writes n in decimal; 20 digits hold every ulong |
| 24 | fn emitu n ulong to void |
| 25 | d = new uchar 20 0 |
| 26 | i = ulong 20 |
| 27 | v = n |
| 28 | while true bound 20 |
| 29 | i = sub i 1 |
| 30 | put d i uchar add rem v 10 48 |
| 31 | v = div v 10 |
| 32 | if eq v 0 |
| 33 | break |
| 34 | end |
| 35 | end |
| 36 | emit part d i sub 20 i |
| 37 | end |
| 38 | note a cell holding 4 or more grains topples: it loses 4 and each of its 4 neighbours gains 1; grains |
| 39 | note pushed past the edge are lost; the end state and the number of topplings do not depend on order |
| 40 | fn give k ulong r long c long n ulong to void |
| 41 | if and and ge r 0 lt r long n and ge c 0 lt c long n |
| 42 | j = add mul ulong r n ulong c |
| 43 | put grid j add get grid j 1 |
| 44 | end |
| 45 | end |
| 46 | fn topple r ulong c ulong n ulong to void |
| 47 | k = add mul r n c |
| 48 | put grid k sub get grid k 4 |
| 49 | put tops 0 add get tops 0 1 |
| 50 | give k sub long r 1 long c n |
| 51 | give k add long r 1 long c n |
| 52 | give k long r sub long c 1 n |
| 53 | give k long r add long c 1 n |
| 54 | end |
| 55 | note sweep topples every full cell once and says whether any toppled |
| 56 | fn sweep n ulong to bool |
| 57 | any = false |
| 58 | for r ulong n |
| 59 | for c ulong n |
| 60 | if ge get grid add mul r n c 4 |
| 61 | topple r c n |
| 62 | any = true |
| 63 | end |
| 64 | end |
| 65 | end |
| 66 | ret any |
| 67 | end |
| 68 | note sweeping in this order takes 6, 25 and 91 sweeps for the three cases (counted on the host); |
| 69 | note 10000 bounds them with room to spare |
| 70 | fn pile n ulong grains ulong to void |
| 71 | for i ulong mul n n |
| 72 | put grid i 0 |
| 73 | end |
| 74 | put tops 0 0 |
| 75 | m = div n 2 |
| 76 | put grid add mul m n m grains |
| 77 | while sweep n bound 10000 |
| 78 | end |
| 79 | for r ulong n |
| 80 | for c ulong n |
| 81 | if gt c 0 |
| 82 | emit sp |
| 83 | end |
| 84 | emitu get grid add mul r n c |
| 85 | end |
| 86 | emit nl |
| 87 | end |
| 88 | emit ttop |
| 89 | emitu get tops 0 |
| 90 | emit nl |
| 91 | end |
| 92 | fn main to int |
| 93 | pile 5 16 |
| 94 | emit nl |
| 95 | pile 7 64 |
| 96 | emit nl |
| 97 | pile 9 200 |
| 98 | ret 0 |
| 99 | end |
| 100 | </syntaxhighlight> |
| 101 | {{out}} |
| 102 | <pre> |
| 103 | 0 0 1 0 0 |
| 104 | 0 2 1 2 0 |
| 105 | 1 1 0 1 1 |
| 106 | 0 2 1 2 0 |
| 107 | 0 0 1 0 0 |
| 108 | topplings: 9 |
| 109 | |
| 110 | 0 0 1 2 1 0 0 |
| 111 | 0 2 2 2 2 2 0 |
| 112 | 1 2 2 2 2 2 1 |
| 113 | 2 2 2 0 2 2 2 |
| 114 | 1 2 2 2 2 2 1 |
| 115 | 0 2 2 2 2 2 0 |
| 116 | 0 0 1 2 1 0 0 |
| 117 | topplings: 88 |
| 118 | ... |
| 119 | 2 3 2 2 3 2 2 3 2 |
| 120 | 3 2 2 2 3 2 2 2 3 |
| 121 | 1 0 3 3 0 3 3 0 1 |
| 122 | 3 2 2 2 3 2 2 2 3 |
| 123 | 2 3 2 2 3 2 2 3 2 |
| 124 | 3 0 3 2 0 2 3 0 3 |
| 125 | 0 3 2 3 1 3 2 3 0 |
| 126 | topplings: 758 |
| 127 | </pre> |