interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / abelian-sandpile-model.wiki

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