maze solving

http://golf.shinh.org/p.rb?maze+solving


maze.rb

$z=gets('').split("\n")
$y=$z[0].length
$x=$z.length

def peek(i,j)
 if i>$x||i<0||j<0||j>$y ;nil
 else ;$z[i][j..j]
 end
end
  
def poke(i,j,v)
 $z[i][j..j]=v
end

def deadnear(i,j)
 an =[]
 an+=[i,j-1] if peek(i,j-1)==" "
 an+=[i,j+1] if peek(i,j+1)==" "
 an+=[i-1,j] if peek(i-1,j)==" "
 an+=[i+1,j] if peek(i+1,j)==" "
 if an.length!=2
  return [false,an]
 end
 [true,an]
end

def dd (a,b,c)
 dn =deadnear(a,b)
  if peek(a,b)==" " && dn[0]
    $z[a][b]="@"[0]
    puts $z
    if dn[1].length==2
     c =dd dn[1][0],dn[1][1],c
    end
    c+=1
  end
  c
end

def deaddel(s)
 c=0
 ($x-2).times{|i|
  ($y-2).times{|j|
   c= dd i+1,j+1,c
  }
 }
 c
end

while deaddel($z)>0;end
$z.each{|i|i.gsub!(/ /,".");i.gsub!(/@/," ")}
puts $z