Skip to content

Instantly share code, notes, and snippets.

@nicolasiensen
Created May 2, 2022 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasiensen/be1c5eb720df7927c6821c33de3839fe to your computer and use it in GitHub Desktop.
Save nicolasiensen/be1c5eb720df7927c6821c33de3839fe to your computer and use it in GitHub Desktop.

Maze escape 🏃

Given a two-dimensional array (the maze) and an array of steps, implement a solution to verify whether or not the player escaped the labyrinth.

  • The solution should output "Escaped" if the player reaches the maze's exit.
  • The solution should output "Dead" if the player hits a wall or goes outside the maze border.
  • The solution should output "Lost forever" if the player does not reach the maze's exit after taking all their steps.

The two-dimensional data structure that represents the maze looks like this:

[
  [1, 1, 1, 1, 1, 1, 1, 1, 0, 1],
  [1, 3, 1, 0, 1, 0, 0, 0, 0, 1],
  [1, 0, 1, 0, 0, 0, 1, 1, 0, 1],
  [1, 0, 1, 1, 1, 1, 1, 0, 0, 1],
  [1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  [1, 0, 1, 0, 1, 0, 1, 0, 0, 1],
  [1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
  [1, 0, 1, 0, 1, 0, 1, 1, 0, 1],
  [1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
  [1, 1, 1, 0, 1, 1, 1, 1, 2, 1]
]

// 0 = Safe place to walk
// 1 = Wall
// 2 = Player start point
// 3 = Maze exit

The list of steps will look like the following:

["N", "W", "W", "W", "N", "N", "N", "N", "W", "W", "S", "S", "S", "S", "W", "W", "N", "N", "N", "N", "N", "N", "N"]

// N = North
// E = East
// W = West
// S = South

Using the maze above, if the player's first step is S, the output should be "Dead" as they leave the maze borders. If the first step is E or W, the player is also dead as they hit a wall. If the first step is N, they are still in the game, and the program can execute the next step.

Credits

https://edabit.com/challenge/HSprZxYCc3KxysAoK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment