interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / abelian-sandpile-model / oracle.py

26 lines · 919 bytes Raw Blame History
1# Independent reference for "Abelian sandpile model" (Python 3, standard library only).
2# expected.txt is this program's stdout; omc never produced it.
3# a work list of full cells, each toppling all its whole fours at once
4def pile(n, grains):
5 g = {(r, c): 0 for r in range(n) for c in range(n)}
6 g[n // 2, n // 2] = grains
7 work, tops = [(n // 2, n // 2)], 0
8 while work:
9 r, c = work.pop()
10 k = g[r, c] // 4
11 if k == 0:
12 continue
13 g[r, c] -= 4 * k
14 tops += k
15 for p in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
16 if p in g:
17 g[p] += k
18 if g[p] >= 4:
19 work.append(p)
20 for r in range(n):
21 print(" ".join(str(g[r, c]) for c in range(n)))
22 print("topplings:", tops)
23for t, (n, grains) in enumerate(((5, 16), (7, 64), (9, 200))):
24 if t:
25 print()
26 pile(n, grains)