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