Skip to content

Instantly share code, notes, and snippets.

@JohnnyJayJay
Created May 23, 2020 20:53
Show Gist options
  • Select an option

  • Save JohnnyJayJay/37b8cdf5d6a3750f82dd732274c87631 to your computer and use it in GitHub Desktop.

Select an option

Save JohnnyJayJay/37b8cdf5d6a3750f82dd732274c87631 to your computer and use it in GitHub Desktop.
def add_adjacent_tiles(grid, room, x, y):
room.append((x, y))
grid[x][y] = 'C'
adjacent_tile_positions = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
for (x, y) in adjacent_tile_positions:
if x < len(grid) and y < len(grid[x]) and grid[x][y] == 'R':
add_adjacent_tiles(grid, room, x, y)
return tiles
def group_tiles_to_rooms(grid):
rooms = []
for x in range(len(grid)):
for y in range(len(grid[x])):
if grid[x][y] == 'R':
room = []
add_adjacent_tiles(grid, room, x, y)
rooms.append(room)
return rooms
grid = list(map(lambda x: list(x), """
0RRRROOOOOOOOOOO
ORRRO0000OO00OOO
ORR0O00RRR000OOO
OOORRRRRRROO0OOO
OOOOOOORROOO0000
""".strip().split("\n")))
print(group_tiles(grid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment