Skip to content

Commit

Permalink
Merge pull request #1 from adefemi171/cli-lazer-maze
Browse files Browse the repository at this point in the history
used CLI to request matrix size
  • Loading branch information
adefemi171 authored Jan 24, 2022
2 parents 137bf31 + 43a41b9 commit 5e2ef1f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# lazer-maze
Lazer Maze Challenge

## How to setup project and run locally
1. Use CLI to request the matrix size.

## How to setup project and run locall

### Clone the repository

Expand Down
56 changes: 36 additions & 20 deletions lazerMaze.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,61 @@
from copy import deepcopy
def get_coord(box_num, dim):
row = box_num // dim
col = box_num % dim
return (row, col)

grids = []
def populate_grid(grid, dim, pos):
print(pos, len(grids), "HERE")
if pos > dim*dim: return
if pos == dim*dim:
grids.append(list(grid))
for i in range(pos, dim*dim):
row, col = get_coord(i, dim)
for choice in ['L', 'R']:
grid[row][col] = choice
populate_grid(grid, dim, pos+1)
grids.append(deepcopy(grid))
return

row, col = get_coord(pos, dim)
for choice in ['L', 'R']:
grid[row][col] = choice
populate_grid(grid, dim, pos+1)

def createMaze(dim):
grid = [[None] * dim for i in range(dim)]
populate_grid(grid, dim, 0)

def is_open_maze(grid, dim, col, row):
if col >= 0 or col < dim and row >= dim:
def is_open_maze(grid, dim, col, row, last_direction):
if (col >= 0 or col < dim) and row >= dim:
return True

if row >= dim or row < 0 or col >= dim or col < 0:
return False

if grid[row][col] == 'L':
return is_open_maze(grid, dim, row, col+1)
if last_direction == 'R': return False
if last_direction == 'L':
return is_open_maze(grid, dim, row+1, col, 'L')
return is_open_maze(grid, dim, row, col+1, 'L')
else:
return is_open_maze(grid, dim, row, col-1)
if last_direction == 'L':
return False
if last_direction == 'R':
return is_open_maze(grid, dim, row+1, col, 'R')
return is_open_maze(grid, dim, row, col-1, 'R')

return False

def calculate_open_mazes(grid, dim):
num_of_open_mazes = 0
for i in range(dim):
if is_open_maze(grid, dim, i, 0):
num_of_open_mazes += 1

return num_of_open_mazes
if is_open_maze(grid, dim, i, 0, 'X'):
return True
return False
# calculate_open_mazes([['R', 'R', 'R'], ['R', 'R', 'R'], ['R', 'R', 'R']], 3)

if __name__ == '__main__':
dimensions = int(input("please put the grid's dimensions: "))
createMaze(dimensions)
result = 0

createMaze(3)
createMaze(4)
for grid in grids:
if calculate_open_mazes(grid, dimensions) > 0:
result += 1

for grid in grids:
calculate_open_mazes(grid, 3)
calculate_open_mazes(grid, 4)
print("The number of open mazes is", result)

0 comments on commit 5e2ef1f

Please sign in to comment.