-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): add solution for challenge #15
- Loading branch information
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Challenge #15: Autonomous robot | ||
|
||
We are programming some **robots** called giftbot 🤖🎁 that autonomously navigate gift warehouses. | ||
|
||
We are creating a function to which we pass: the warehouse 🏬 they must navigate and the movements ↔️ they can make. | ||
|
||
The warehouse is represented as an **array of text strings**, where: | ||
|
||
- `.` means there is a clear path. | ||
- `*` means there is an obstacle. | ||
- `!` is the robot's initial position. | ||
|
||
The movements are an **array of text strings**, where: | ||
|
||
- `R` moves the robot one position to the right. | ||
- `L` moves the robot one position to the left. | ||
- `U` moves the robot one position upwards. | ||
- `D` moves the robot one position downwards. | ||
|
||
It must be taken into account that **the robot cannot overcome obstacles or the warehouse boundaries.** | ||
|
||
Given a warehouse and the movements, we need to return the array with the robot's final position. | ||
|
||
```js | ||
const store = ['..!....', '...*.*.'] | ||
|
||
const movements = ['R', 'R', 'D', 'L'] | ||
const result = autonomousDrive(store, movements) | ||
console.log(result) | ||
/* | ||
[ | ||
".......", | ||
"...*!*." | ||
] | ||
*/ | ||
|
||
// The last movement is to the left, but it cannot move because there is an obstacle. | ||
``` | ||
|
||
Keep in mind that the `store` is **an array that can have a number of rows ranging from 1 to 100**, as we have warehouses of all sizes. | ||
|
||
Also note that the robot **might end up in its initial position** if it can't move or if it's going around in circles. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe } from 'vitest' | ||
import { autonomousDrive } from './challenge-15' | ||
|
||
const TEST_CASES: TestCases<[string[], string[]], string[]> = [ | ||
{ args: [['..!....'], ['R', 'L']], expected: ['..!....'] }, | ||
{ | ||
args: [ | ||
['!..', '***'], | ||
['U', 'L'], | ||
], | ||
expected: ['!..', '***'], | ||
}, | ||
{ | ||
args: [ | ||
['!..', '***'], | ||
['R', 'L'], | ||
], | ||
expected: ['!..', '***'], | ||
}, | ||
{ | ||
args: [ | ||
['..!....', '......*'], | ||
['R', 'D', 'L'], | ||
], | ||
expected: ['.......', '..!...*'], | ||
}, | ||
{ | ||
args: [ | ||
['*..!..*', '*.....*'], | ||
['R', 'R', 'R', 'D', 'D'], | ||
], | ||
expected: ['*.....*', '*....!*'], | ||
}, | ||
{ | ||
args: [ | ||
['***', '.!.', '***'], | ||
['D', 'U', 'R', 'R', 'R'], | ||
], | ||
expected: ['***', '..!', '***'], | ||
}, | ||
{ | ||
args: [ | ||
['***', '*!*', '***'], | ||
['D', 'U', 'R', 'L'], | ||
], | ||
expected: ['***', '*!*', '***'], | ||
}, | ||
{ | ||
args: [ | ||
['.**.*.*.', '.***....', '..!.....'], | ||
['D', 'U', 'R', 'R', 'R'], | ||
], | ||
expected: ['.**.*.*.', '.***....', '.....!..'], | ||
}, | ||
] | ||
|
||
describe('Challenge #15: Autonomous robot', () => { | ||
buildChallengeTestCases({ | ||
cases: TEST_CASES, | ||
spreadFn: autonomousDrive, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export function autonomousDrive(store: string[], movements: string[]) { | ||
let row = store.findIndex(line => line.includes('!')) | ||
let col = store[row].indexOf('!') | ||
|
||
function placeRobot(robot: string) { | ||
const line = store[row] | ||
store[row] = line.slice(0, col) + robot + line.slice(col + 1) | ||
} | ||
|
||
placeRobot('.') | ||
|
||
const maxRow = store.length | ||
const maxCol = store[0].length | ||
|
||
for (const move of movements) { | ||
const rowMove = row + +(move === 'D') - +(move === 'U') | ||
const colMove = col + +(move === 'R') - +(move === 'L') | ||
const rowValues = [row, row, rowMove] | ||
const colValues = [col, col, colMove] | ||
|
||
const nextRow = rowValues[+(rowMove >= 0) + +(rowMove < maxRow)] | ||
const nextCol = colValues[+(colMove >= 0) + +(colMove < maxCol)] | ||
|
||
const pos = store[nextRow][nextCol] | ||
|
||
if (pos !== '*') { | ||
col = nextCol | ||
row = nextRow | ||
} | ||
} | ||
|
||
placeRobot('!') | ||
|
||
return store | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters