June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,75 @@
using Distributions
newgrid(p::Float64, M::Int=15, N::Int=15) = rand(Bernoulli(p), M, N)
function walkmaze!(grid::Matrix{Int}, r::Int, c::Int, indx::Int)
NOT_VISITED = 1 # const
N, M = size(grid)
dirs = [[1, 0], [-1, 0], [0, 1], [1, 0]]
# fill cell
grid[r, c] = indx
# is the bottom line?
rst = r == N
# for each direction, if has not reached the bottom yet and can continue go to that direction
for d in dirs
rr, cc = (r, c) .+ d
if !rst && checkbounds(Bool, grid, rr, cc) && grid[rr, cc] == NOT_VISITED
rst = walkmaze!(grid, rr, cc, indx)
end
end
return rst
end
function checkpath!(grid::Matrix{Int})
NOT_VISITED = 1 # const
N, M = size(grid)
walkind = 1
for m in 1:M
if grid[1, m] == NOT_VISITED
walkind += 1
if walkmaze!(grid, 1, m, walkind)
return true
end
end
end
return false
end
function printgrid(G::Matrix{Int})
LETTERS = vcat(' ', '#', 'A':'Z')
for r in 1:size(G, 1)
println(r % 10, ") ", join(LETTERS[G[r, :] .+ 1], ' '))
end
if any(G[end, :] .> 1)
println("!) ", join((ifelse(c > 1, LETTERS[c+1], ' ') for c in G[end, :]), ' '))
end
end
const nrep = 1000 # const
sampleprinted = false
p = collect(0.0:0.1:1.0)
f = similar(p)
for i in linearindices(f)
c = 0
for _ in 1:nrep
G = newgrid(p[i])
perc = checkpath!(G)
if perc
c += 1
if !sampleprinted
@printf("Sample percolation, %i×%i grid, p = %.2f\n\n", size(G, 1), size(G, 2), p[i])
printgrid(G)
sampleprinted = true
end
end
end
f[i] = c / nrep
end
println("\nFrequencies for $nrep tries that percolate through\n")
for (pi, fi) in zip(p, f)
@printf("p = %.1f ⇛ f = %.3f\n", pi, fi)
end

View file

@ -0,0 +1,67 @@
// version 1.2.10
import java.util.Random
val rand = Random()
const val RAND_MAX = 32767
const val NUL = '\u0000'
val x = 15
val y = 15
var grid = StringBuilder((x + 1) * (y + 1) + 1)
var cell = 0
var end = 0
var m = 0
var n = 0
fun makeGrid(p: Double) {
val thresh = (p * RAND_MAX).toInt()
m = x
n = y
grid.setLength(0) // clears grid
grid.setLength(m + 1) // sets first (m + 1) chars to NUL
end = m + 1
cell = m + 1
for (i in 0 until n) {
for (j in 0 until m) {
val r = rand.nextInt(RAND_MAX + 1)
grid.append(if (r < thresh) '+' else '.')
end++
}
grid.append('\n')
end++
}
grid[end - 1] = NUL
end -= ++m // end is the index of the first cell of bottom row
}
fun ff(p: Int): Boolean { // flood fill
if (grid[p] != '+') return false
grid[p] = '#'
return p >= end || ff(p + m) || ff(p + 1) || ff(p - 1) || ff(p - m)
}
fun percolate(): Boolean {
var i = 0
while (i < m && !ff(cell + i)) i++
return i < m
}
fun main(args: Array<String>) {
makeGrid(0.5)
percolate()
println("$x x $y grid:")
println(grid)
println("\nrunning 10,000 tests for each case:")
for (ip in 0..10) {
val p = ip / 10.0
var cnt = 0
for (i in 0 until 10_000) {
makeGrid(p)
if (percolate()) cnt++
}
println("p = %.1f: %.4f".format(p, cnt / 10000.0))
}
}