-
-
Save IndianGuru/fd33bb34c6132160e9f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Maze | |
WALL = '#' | |
FREE = ' ' | |
def initialize(maze) | |
@wip_maze = maze.split("\n").collect { |row| row.split('') } | |
solve | |
end | |
def solvable? | |
steps > 0 | |
end | |
attr_writer :steps | |
def steps | |
@steps ||= calc_steps | |
end | |
def calc_steps | |
spaces = @wip_maze.flatten.inject(0) { |s,c| s += (c == FREE) ? 1 : 0 } | |
spaces > 0 ? spaces + 1 : 0 | |
end | |
def solve | |
@wip_maze.each_index do |r| | |
@wip_maze[r].each_index do |c| | |
fill_from_deadend?(r,c) | |
end | |
end | |
end | |
def fill_from_deadend?(r,c) | |
return false unless @wip_maze[r][c] == FREE | |
n = (r == 0) || (@wip_maze[r-1][c] == WALL) ? 1 : 0 | |
s = (r == @wip_maze.size-1) || (@wip_maze[r+1][c] == WALL) ? 1 : 0 | |
e = (c == 0) || (@wip_maze[r][c-1] == WALL) ? 1 : 0 | |
w = (c == @wip_maze[r].size-1) || (@wip_maze[r][c+1] == WALL) ? 1 : 0 | |
if (n+s+e+w)>2 | |
@wip_maze[r][c] = WALL | |
fill_from_deadend?(r-1,c) if n==0 | |
fill_from_deadend?(r+1,c) if s==0 | |
fill_from_deadend?(r,c-1) if e==0 | |
fill_from_deadend?(r,c+1) if w==0 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment