RosettaCodeData/Task/Percolation-Bond-percolation/Phix/percolation-bond-percolation.phix
2019-09-12 10:33:56 -07:00

55 lines
1.5 KiB
Text

constant w = 10, h = 10
sequence wall = join(repeat("+",w+1),"---")&"\n",
cell = join(repeat("|",w+1)," ")&"\n",
grid
procedure new_grid(atom p)
grid = split(join(repeat(wall,h+1),cell),'\n')
-- now knock down some walls
for i=1 to length(grid)-1 do
integer jstart = 5-mod(i,2)*3,
jlimit = length(grid[i])-3
-- (ie 2..38 on odd lines, 5..37 on even)
for j=jstart to jlimit by 4 do
if rnd()>p then
grid[i][j..j+2] = " "
end if
end for
end for
end procedure
function percolate(integer x=0, y=0)
if x=0 then
for j=3 to length(grid[1])-2 by 4 do
if grid[1][j]=' ' and percolate(1,j) then
return true
end if
end for
elsif grid[x][y]=' ' then
grid[x][y] = '*'
if (x=length(grid)-1)
or ( grid[x+1][y]=' ' and percolate(x+1,y))
or (y>6 and grid[x][y-2]=' ' and percolate(x,y-4))
or (y<36 and grid[x][y+2]=' ' and percolate(x,y+4))
or (x>1 and grid[x-1][y]=' ' and percolate(x-1,y)) then
return true
end if
end if
return false
end function
constant LIM=1000
for p=0 to 10 do
integer count = 0
for t=1 to LIM do
new_grid(p/10)
count += percolate()
end for
printf(1,"p=%.1f: %5.3f\n",{p/10,count/LIM})
end for
puts(1,"sample grid for p=0.6:\n")
new_grid(0.6)
{} = percolate()
puts(1,join(grid,'\n'))