Skip to content

Instantly share code, notes, and snippets.

@vrootic
Last active September 7, 2017 02:51
Show Gist options
  • Save vrootic/844027b14fcd534c0fbbaebff0c7aeb6 to your computer and use it in GitHub Desktop.
Save vrootic/844027b14fcd534c0fbbaebff0c7aeb6 to your computer and use it in GitHub Desktop.
# Your previous Python 2 content is preserved below:
#
# ### ASCII Maze Master
#
# # Write a program that will find a path through a simple maze. A simple maze in this context is a maze where all of the walls are connected to each other. Take this example maze segment.
#
# '''
# # # ### #
# # #
# # ### B #
# # # B #
# # B # B #
# # B B #
# # BBBBB #
# # #
# #########
# '''
#
# # See how the wall drawn with Bs isn't connected to any other walls? That's called a floating wall. A simple maze contains no floating walls - ie. there are no loops in the maze.
#
# #### Formal Inputs and Outputs
#
# ##### Input Description
#
# # You will be given two numbers X and Y. After that you will be given a textual ASCII grid, X wide and Y tall, of walls # and spaces. In the maze there will be exactly one letter S and exactly one letter E. There will be no spaces leading to the outside of the maze - ie. it will be fully walled in.
#
# ##### Output Description
#
# # You must print out the maze. Within the maze there should be a path drawn with `.` leading from the letter S to the letter E. Try to minimise the length of the path if possible - don't just fill all of the spaces with `.`!
#
# # Sample Inputs & Output
#
# ##### Sample Input
#
# # 15x15
# '''
# ###############
# #S # #
# ### ### ### # #
# # # # # #
# # ##### ##### #
# # # # #
# # ### # ### ###
# # # # # # #
# # # ### # ### #
# # # # # # # #
# ### # # # # # #
# # # # # # #
# # ####### # # #
# # #E#
# ###############
# '''
#
# # Sample Output
#
# '''
# ###############
# #S.. # #
# ###.### ### # #
# #...# # # #
# #.##### ##### #
# #.....# # #
# # ###.# ### ###
# # #...# # # #
# # #.### # ### #
# # #.# # # #...#
# ###.# # # #.#.#
# #...# # #.#.#
# #.####### #.#.#
# #...........#E#
# ###############
# '''
#
# # One easy way to solve simple mazes is to always follow the wall to your left or right. You will eventually arrive at the end.
#
maze = '''
###############
#S # #
### ### ### # #
# # # # #
# ##### ##### #
# # # #
# ### # ### ###
# # # # # #
# # ### # ### #
# # # # # # #
### # # # # # #
# # # # # #
# ####### # # #
# #E#
###############
'''
maze_repr = []
width = 15
length = 15
for line in maze.strip().splitlines():
maze_repr.append([c for c in line])
def check_surround(x, y):
global maze_repr
steps = [[-1, 0], [0, 1], [1, 0], [0, -1]]
flag = 1
count = 0
while flag:
flag = 0
for i in range(length):
for j in range(width):
directions = 0
for s in steps:
if maze_repr[i][j] == ' ':
if maze_repr[i+s[1]][j+s[0]] != 'X':
if maze_repr[i+s[1]][j+s[0]] != '#':
directions += 1
if directions == 1:
maze_repr[i][j] = 'X'
flag = 1
count += 1
print("count = %d" % count)
for i in range(length):
for j in range(width):
if maze_repr[i][j] == ' ':
maze_repr[i][j] = '*'
if maze_repr[i][j] == 'X':
maze_repr[i][j] = ' '
return
check_surround(1, 1)
for line in maze_repr:
print("".join(line))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment