Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maze runner #206

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions exercises/practice/maze-runner/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Instructions

The entrance and exit to the labyrinth are marked with the same symbol, `⇨`.
[Box-drawing][] characters draw the maze, while spaces represent the path.
Your task is to mark the way from the entrance to the exit in the maze with the dots.

For example, the following maze:

```text
────┬─────────┬─────────┬───────┬───┐
⇨ │ │ │ │ │
┌─┐ └─┐ ┌─┐ │ └─┐ ──┬── │ ──┐ │ │ ──┘
│ │ │ │ │ │ │ │ │ │ │ │ ⇨
│ └─┐ │ │ │ ├─┐ └─┐ └─┐ │ ┌─┘ │ │ │ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ──┘ ├───┘ │ └── │ │ └─┘ │ ┌─┘ │ │ │
│ │ │ │ │ │ │ │ │ │
│ ────┤ ────┴───┐ ├─┴───┐ │ │ ┌─┴─┤ │
│ │ │ │ │ │ │ │ │ │
├──── └──────── │ │ ┌── └─┘ │ │ │ │ │
│ │ │ │ │ │
└───────────────┴───┴───────┴───┴───┘
```

Should be marked like this:

```text
────┬─────────┬─────────┬───────┬───┐
⇨⋅⋅⋅│ ⋅⋅⋅│ │ ⋅⋅⋅│ │
┌─┐⋅└─┐ ┌─┐⋅│⋅└─┐ ──┬── │ ──┐⋅│⋅│ ──┘
│ │⋅⋅⋅│ │ │⋅│⋅⋅⋅│ │ │ │⋅│⋅│ ⋅⇨
│ └─┐⋅│ │ │⋅├─┐⋅└─┐ └─┐ │ ┌─┘⋅│⋅│ │⋅│
│ │⋅│ │⋅│ │⋅⋅⋅│ │ │ │⋅⋅⋅│⋅│ │⋅│
│ ──┘⋅├───┘⋅│ └──⋅│ │ └─┘ │⋅┌─┘⋅│ │⋅│
│⋅⋅⋅⋅⋅│⋅⋅⋅⋅⋅│ ⋅│ │ │⋅│⋅⋅⋅│ │⋅│
│⋅────┤⋅────┴───┐⋅├─┴───┐ │⋅│⋅┌─┴─┤⋅│
│⋅⋅⋅⋅⋅│⋅⋅⋅⋅⋅⋅⋅⋅⋅│⋅│⋅⋅⋅⋅⋅│ │⋅│⋅│⋅⋅⋅│⋅│
├────⋅└────────⋅│⋅│⋅┌──⋅└─┘⋅│⋅│⋅│⋅│⋅│
│ ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅│⋅⋅⋅│ ⋅⋅⋅⋅⋅│⋅⋅⋅│⋅⋅⋅│
└───────────────┴───┴───────┴───┴───┘
```
[Box-drawing]: https://en.wikipedia.org/wiki/Box-drawing_character
7 changes: 7 additions & 0 deletions exercises/practice/maze-runner/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

Bob is a [lackadaisical][] teenager with a thing for tackling mazes but often finds himself stuck and disheartened.
In this exercise, you'll develop a program that guides Bob through the twists and turns of any labyrinth, ensuring he overcomes obstacles and retains his enthusiasm for maze-solving.
Help Bob conquer the mazes with your programming skills!

[lackadaisical]: https://www.collinsdictionary.com/dictionary/english/lackadaisical
19 changes: 19 additions & 0 deletions exercises/practice/maze-runner/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"rabestro"
],
"files": {
"solution": [
"maze-runner.awk"
],
"test": [
"test-maze-runner.bats"
],
"example": [
".meta/example.awk"
]
},
"blurb": "Bob is a lackadaisical teenager with a thing for tackling mazes but often finds himself stuck and disheartened. ",
"source": "Inspired by the 'Maze Runner' exercise in Hyperskill's Java Core Track.",
"source_url": "https://hyperskill.org/projects/47?category=2&track=15"
}
57 changes: 57 additions & 0 deletions exercises/practice/maze-runner/.meta/example.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
BEGIN {
FPAT = "."
OFS = ""
PathMark = "⋅"
EmptyCell = " "
}

NR == 1 {
Width = NF
}
{
++Height
}
$1 == "⇨" {
StartRow = Height
StartCol = 2
}
$NF == "⇨" {
EndRow = Height
EndCol = Width - 1
}
{
for (; NF; --NF) Grid[Height, NF] = $NF
}

END {
if (!find_path(StartRow, StartCol))
die("Path not found")
print_maze()
}

function find_path(row, col) {
if (is_not_free(row, col)) return 0
Grid[row, col] = PathMark
if (is_path_valid(row, col)) return 1
Grid[row, col] = " "
return 0
}
function is_not_free(row, col) {
return row < 1 || row > Height || col < 1 || col > Width ||
Grid[row, col] != EmptyCell
}
function is_path_valid(row, col) {
return row == EndRow && col == EndCol ||
find_path(row + 1, col) ||
find_path(row - 1, col) ||
find_path(row, col + 1) ||
find_path(row, col - 1)
}
function print_maze( row, col) {
while (row++ < Height) {
for (col = Width; col; --col)
$col = Grid[row, col]
print
}
}
function die(message) {print message > "/dev/stderr"; exit 1}
4 changes: 4 additions & 0 deletions exercises/practice/maze-runner/maze-runner.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BEGIN {
print "Implement this solution" > "/dev/stderr"
exit 1
}
42 changes: 42 additions & 0 deletions exercises/practice/maze-runner/test-maze-runner.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bats

@test "maze-runner.awk produces expected output" {
# Input maze
maze=$(cat << EOF
────┬─────────┬─────────┬───────┬───┐
⇨ │ │ │ │ │
┌─┐ └─┐ ┌─┐ │ └─┐ ──┬── │ ──┐ │ │ ──┘
│ │ │ │ │ │ │ │ │ │ │ │ ⇨
│ └─┐ │ │ │ ├─┐ └─┐ └─┐ │ ┌─┘ │ │ │ │
│ │ │ │ │ │ │ │ │ │ │ │ │ │
│ ──┘ ├───┘ │ └── │ │ └─┘ │ ┌─┘ │ │ │
│ │ │ │ │ │ │ │ │ │
│ ────┤ ────┴───┐ ├─┴───┐ │ │ ┌─┴─┤ │
│ │ │ │ │ │ │ │ │ │
├──── └──────── │ │ ┌── └─┘ │ │ │ │ │
│ │ │ │ │ │
└───────────────┴───┴───────┴───┴───┘
EOF
)

expected_output=$(cat << EOF
────┬─────────┬─────────┬───────┬───┐
⇨⋅⋅⋅│ ⋅⋅⋅│ │ ⋅⋅⋅│ │
┌─┐⋅└─┐ ┌─┐⋅│⋅└─┐ ──┬── │ ──┐⋅│⋅│ ──┘
│ │⋅⋅⋅│ │ │⋅│⋅⋅⋅│ │ │ │⋅│⋅│ ⋅⇨
│ └─┐⋅│ │ │⋅├─┐⋅└─┐ └─┐ │ ┌─┘⋅│⋅│ │⋅│
│ │⋅│ │⋅│ │⋅⋅⋅│ │ │ │⋅⋅⋅│⋅│ │⋅│
│ ──┘⋅├───┘⋅│ └──⋅│ │ └─┘ │⋅┌─┘⋅│ │⋅│
│⋅⋅⋅⋅⋅│⋅⋅⋅⋅⋅│ ⋅│ │ │⋅│⋅⋅⋅│ │⋅│
│⋅────┤⋅────┴───┐⋅├─┴───┐ │⋅│⋅┌─┴─┤⋅│
│⋅⋅⋅⋅⋅│⋅⋅⋅⋅⋅⋅⋅⋅⋅│⋅│⋅⋅⋅⋅⋅│ │⋅│⋅│⋅⋅⋅│⋅│
├────⋅└────────⋅│⋅│⋅┌──⋅└─┘⋅│⋅│⋅│⋅│⋅│
│ ⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅⋅│⋅⋅⋅│ ⋅⋅⋅⋅⋅│⋅⋅⋅│⋅⋅⋅│
└───────────────┴───┴───────┴───┴───┘
EOF
)

# Run the script and compare the output to the expected output
result="$(echo "$maze" | gawk -f maze-runner.awk)"
[ "$result" == "$expected_output" ]
}