Last active
August 29, 2015 14:10
-
-
Save Jberczel/5518fd105f29d08d6392 to your computer and use it in GitHub Desktop.
implementation of flood fill algorithm
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 FloorPlan | |
attr_reader :input, :layout, :rows, :cols | |
EMPTY = '.' | |
VISITED = '-' | |
def initialize(file) | |
@input = format(file) | |
@layout = format(file) | |
@rows = @layout.size | |
@cols = @layout[0].size | |
end | |
def rooms | |
@rooms ||= count_rooms | |
end | |
def display | |
input.map { |row| row.join }.join("\n") | |
end | |
private | |
def count_rooms | |
count = 0 | |
each { |r,c| count += 1 if floodfill(r,c,EMPTY, VISITED) } | |
# minus 1 since the area outside of all rooms counts as a “room” | |
count - 1 | |
end | |
def floodfill(x, y, old_sym, new_sym) | |
return unless valid?(x,y) && layout[x][y] == old_sym | |
layout[x][y] = new_sym | |
floodfill(x+1, y, old_sym, new_sym) | |
floodfill(x-1, y, old_sym, new_sym) | |
floodfill(x, y+1, old_sym, new_sym) | |
floodfill(x, y-1, old_sym, new_sym) | |
true | |
end | |
def format(file) | |
open(file, 'r') { |f| f.read }.split.map(&:chars) | |
end | |
def each(&block) | |
layout.each_with_index do |row, r| | |
row.each_index { |c| block.call(r,c) } | |
end | |
end | |
def valid?(x,y) | |
x.between?(0, rows-1) && | |
y.between?(0, cols-1) | |
end | |
end | |
display = ->(msg, file) do | |
puts msg | |
puts '------------' | |
fp = FloorPlan.new(file) | |
puts "# of rooms: #{fp.rooms}" | |
puts fp.display | |
print "\n\n" | |
end | |
display["Floor Plan 1", 'fp_1.txt'] | |
display["Floor Plan 2", 'fp_2.txt'] | |
display["Floor Plan 3", 'fp_3.txt'] | |
display["Floor Plan 4", 'fp_4.txt'] | |
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
........................#..... | |
........................#..... | |
........................#..... | |
.....######.............###### | |
.....#....#....######......... | |
.....#....#....#....#.....#... | |
.....######....##.###.....#... | |
................#.#.......#### | |
................#.#........... | |
######.....######.#######..... | |
#....#.....#............#..... | |
###..#.....#............#..... | |
###..#.....######.#######..... | |
#....#.....#.................. | |
######.....#.................. |
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
........................#..... | |
........................###### | |
........................#..... | |
.....######.............###### | |
.....#....#....######......... | |
.....#....#....#....#.....#... | |
.....######....##.###.....#... | |
................#.#.......#### | |
................#.#........... | |
######.....######.#######..... | |
#....#.....#............#..... | |
###..#.....#............#..... | |
###..#.....##############..... | |
#....#....................#### | |
######....................#... |
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
...#..#... | |
####..#... | |
......#### | |
......#... | |
......#... |
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
........................#..... | |
........................#..... | |
........................#..... | |
.....######.............###### | |
.....#....#....######......... | |
.....#....#....#....#.....#### | |
.....######....######.....#... | |
..........................#### | |
.............................. | |
######.....##############..... | |
#....#.....#............#..... | |
######.....#............#..... | |
#....#.....##############..... | |
#....#........................ | |
######........................ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment