From 43a41b97ee851e7fdfe7118c55bda147119d762d Mon Sep 17 00:00:00 2001 From: adefemi171 Date: Mon, 24 Jan 2022 13:47:45 +0100 Subject: [PATCH] used CLI to request matrix size --- README.md | 4 +++- lazerMaze.py | 56 +++++++++++++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9982dd5..66ff98e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lazerMaze.py b/lazerMaze.py index 7ddadfe..b1d9bf3 100644 --- a/lazerMaze.py +++ b/lazerMaze.py @@ -1,3 +1,4 @@ +from copy import deepcopy def get_coord(box_num, dim): row = box_num // dim col = box_num % dim @@ -5,41 +6,56 @@ def get_coord(box_num, dim): 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) \ No newline at end of file + print("The number of open mazes is", result) \ No newline at end of file