Abelian sandpile model/Identity: Difference between revisions
Added 11l
Alextretyak (talk | contribs) (Added 11l) |
|||
Line 71:
* https://en.wikipedia.org/wiki/Abelian_sandpile_model
<br><br>
=={{header|11l}}==
{{trans|Python}}
<lang 11l>T Sandpile
DefaultDict[(Int, Int), Int] grid
F (gridtext)
V array = gridtext.split_py().map(x -> Int(x))
L(x) array
.grid[(L.index I/ 3, L.index % 3)] = x
Set[(Int, Int)] _border = Set(cart_product(-1 .< 4, -1 .< 4).filter((r, c) -> !(r C 0..2) | !(c C 0..2)))
_cell_coords = cart_product(0.<3, 0.<3)
F topple()
V& g = .grid
L(r, c) ._cell_coords
I g[(r, c)] >= 4
g[(r - 1, c)]++
g[(r + 1, c)]++
g[(r, c - 1)]++
g[(r, c + 1)]++
g[(r, c)] -= 4
R 1B
R 0B
F stabilise()
L .topple() {}
L(row_col) ._border.intersection(Set(.grid.keys()))
.grid.pop(row_col)
F ==(other)
R all(._cell_coords.map(row_col -> @.grid[row_col] == @other.grid[row_col]))
F +(other)
V ans = Sandpile(‘’)
L(row_col) ._cell_coords
ans.grid[row_col] = .grid[row_col] + other.grid[row_col]
ans.stabilise()
R ans
F String()
[String] txt
L(row) 3
txt.append((0.<3).map(col -> String(@.grid[(@row, col)])).join(‘ ’))
R txt.join("\n")
V unstable = Sandpile(‘4 3 3
3 1 2
0 2 3’)
V s1 = Sandpile(‘1 2 0
2 1 1
0 1 3’)
V s2 = Sandpile(‘2 1 3
1 0 1
0 1 0’)
V s3 = Sandpile(‘3 3 3 3 3 3 3 3 3’)
V s3_id = Sandpile(‘2 1 2 1 0 1 2 1 2’)
print(unstable)
print()
unstable.stabilise()
print(unstable)
print()
print(s1 + s2)
print()
print(s2 + s1)
print()
print(s1 + s2 == s2 + s1)
print()
print(s3 + s3_id)
print()
print(s3 + s3_id == s3)
print()
print(s3_id + s3_id)
print()
print(s3_id + s3_id == s3_id)</lang>
{{out}}
<pre>
4 3 3
3 1 2
0 2 3
2 1 0
0 3 3
1 2 3
3 3 3
3 1 2
0 2 3
3 3 3
3 1 2
0 2 3
1B
3 3 3
3 3 3
3 3 3
1B
2 1 2
1 0 1
2 1 2
1B
</pre>
=={{header|AArch64 Assembly}}==
Line 410 ⟶ 522:
2 1 2
</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
|