pdamer / pauldamer.rb secret
Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View pauldamer.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# An implementation to see if you could solve it just using regular expressions
# Paul Damer
 
class Maze
DEBUG=false
def initialize(maze)
@maze = maze.dup
@steps = 1
@width = @maze.index("\n")
@solvable = nil
#detects an A next to a B
@a_by_b = %r{BA|AB|A.{#{@width}}B|B.{#{@width}}A}m
#detects a space next to an A
@a_ajacent = %r{ (?=A)|(?<=A) | (?=.{#{@width}}A)|(?<=A.{#{@width}}) }m
#detects a space surrounded on 3 sides
@dead_end = %r{
(?#open_right)(?<=\#.{#{@width-1}}\#)\ (?=\ .{#{@width-1}}\#)|
(?#open_down) (?<=\#.{#{@width-1}}\#)\ (?=\#.{#{@width-1}}\ )|
(?#open_left) (?<=\#.{#{@width-1}}\ )\ (?=\#.{#{@width-1}}\#)|
(?#open_up) (?<=\ .{#{@width-1}}\#)\ (?=\#.{#{@width-1}}\#)
}mx
end
def steps
solvable? ? @steps : 0
end
def solvable?
return @solveable unless @solvable.nil?
remove_dead_ends unless a_is_by_b?
@solvable = can_get_to_b_from_a?
end
protected
def a_is_by_b?
@a_is_by_b ||= @maze.match(@a_by_b)
end
#Fill in As until you reach B
def can_get_to_b_from_a?
while !a_is_by_b? and @maze.gsub!(@a_ajacent, 'A') do
@steps += 1
puts @maze if DEBUG
end
a_is_by_b? ? true : false
end
def remove_dead_ends
while @maze.gsub!(@dead_end, "#") do
puts @maze if DEBUG
end
end
 
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.