interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / si / rosetta / tasks / abelian-sandpile-model / abelian-sandpile-model.om

97 lines · 2073 bytes Raw Blame History
1note Rosetta Code task "Abelian sandpile model": https://rosettacode.org/wiki/Abelian_sandpile_model
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text sp " "
6text minus "-"
7text ttop "topplings: "
8global grid ulong 81
9global tops ulong 1
10note emit writes every byte of a to stdout; a short write is retried, an error exits 1
11fn emit a view uchar * to void
12o = ulong 0
13while lt o len a bound 4096
14k = write 1 a o sub len a o
15if le k 0
16exit 1
17end
18o = add o ulong k
19end
20end
21note emitu writes n in decimal; 20 digits hold every ulong
22fn emitu n ulong to void
23d = new uchar 20 0
24i = ulong 20
25v = n
26while true bound 20
27i = sub i 1
28put d i uchar add rem v 10 48
29v = div v 10
30if eq v 0
31break
32end
33end
34emit part d i sub 20 i
35end
36note a cell holding 4 or more grains topples: it loses 4 and each of its 4 neighbours gains 1; grains
37note pushed past the edge are lost; the end state and the number of topplings do not depend on order
38fn give k ulong r long c long n ulong to void
39if and and ge r 0 lt r long n and ge c 0 lt c long n
40j = add mul ulong r n ulong c
41put grid j add get grid j 1
42end
43end
44fn topple r ulong c ulong n ulong to void
45k = add mul r n c
46put grid k sub get grid k 4
47put tops 0 add get tops 0 1
48give k sub long r 1 long c n
49give k add long r 1 long c n
50give k long r sub long c 1 n
51give k long r add long c 1 n
52end
53note sweep topples every full cell once and says whether any toppled
54fn sweep n ulong to bool
55any = false
56for r ulong n
57for c ulong n
58if ge get grid add mul r n c 4
59topple r c n
60any = true
61end
62end
63end
64ret any
65end
66note sweeping in this order takes 6, 25 and 91 sweeps for the three cases (counted on the host);
67note 10000 bounds them with room to spare
68fn pile n ulong grains ulong to void
69for i ulong mul n n
70put grid i 0
71end
72put tops 0 0
73m = div n 2
74put grid add mul m n m grains
75while sweep n bound 10000
76end
77for r ulong n
78for c ulong n
79if gt c 0
80emit sp
81end
82emitu get grid add mul r n c
83end
84emit nl
85end
86emit ttop
87emitu get tops 0
88emit nl
89end
90fn main to int
91pile 5 16
92emit nl
93pile 7 64
94emit nl
95pile 9 200
96ret 0
97end