Skip to content

Commit

Permalink
feat(2023): add solution for challenge #15
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 15, 2023
1 parent 6580b52 commit 16d12a3
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
42 changes: 42 additions & 0 deletions 2023/challenge-15/README.md
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.
62 changes: 62 additions & 0 deletions 2023/challenge-15/challenge-15.test.ts
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,
})
})
35 changes: 35 additions & 0 deletions 2023/challenge-15/challenge-15.ts
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
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
| 12 | Is it a valid copy? | 🟠 | [Show](https://adventjs.dev/challenges/2023/12) | [Go](./2023/challenge-12/challenge-12.ts) |
| 13 | Calculating the time | 🟢 | [Show](https://adventjs.dev/challenges/2023/13) | [Go](./2023/challenge-13/challenge-13.ts) |
| 14 | Avoid the alarm | 🟠 | [Show](https://adventjs.dev/challenges/2023/14) | [Go](./2023/challenge-14/challenge-14.ts) |
| 15 | -- | -- | -- | -- |
| 15 | Autonomous robot | 🟠 | [Show](https://adventjs.dev/challenges/2023/15) | [Go](./2023/challenge-15/challenge-15.ts) |
| 16 | -- | -- | -- | -- |
| 17 | -- | -- | -- | -- |
| 18 | -- | -- | -- | -- |
Expand Down

0 comments on commit 16d12a3

Please sign in to comment.