Skip to content

Commit

Permalink
feat(2023): add solution for challenge #06
Browse files Browse the repository at this point in the history
  • Loading branch information
iswilljr committed Dec 6, 2023
1 parent a857869 commit 20440ba
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
33 changes: 33 additions & 0 deletions 2023/challenge-06/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Challenge #6: The reindeer on trial

The elves are cataloging Santa's reindeer 🦌 based on the distance they can travel.

For this, they have a text string `movements` where each character represents the direction of the reindeer's movement:

- **`>` = Moves to the right**
- **`<` = Moves to the left**
- **`*` = Can move forward or backward**

For example, if the movement is `>>*<`, it goes to the right twice, then it can go either left or right (whichever maximizes the final traveled distance) and then left.

The elves want to know what the maximum distance the reindeer travels is **after completing all movements.**

**In the previous example, the maximum distance the reindeer travels is `2`.** It goes to the right twice `+2`, then with the `*` it can go to the right again to maximize the distance `+1` and then it goes to the left `-1`.

Create a `maxDistance` function that takes the text string `movements` and returns **the maximum distance** that the reindeer can travel **in any direction**:

```js
const movements = '>>*<'
const result = maxDistance(movements)
console.log(result) // -> 2

const movements2 = '<<<>'
const result2 = maxDistance(movements2)
console.log(result2) // -> 2

const movements3 = '>***>'
const result3 = maxDistance(movements3)
console.log(result3) // -> 5
```

Keep in mind that it doesn't matter whether it is to the left or right, the distance is **the absolute value of the maximum distance traveled at the end of the movements.**
17 changes: 17 additions & 0 deletions 2023/challenge-06/challenge-06.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe } from 'vitest'
import { maxDistance } from './challenge-06'

const TEST_CASES: TestCases<string, number> = [
{ args: '>>*<', expected: 2 },
{ args: '<<<<<', expected: 5 },
{ args: '>***>', expected: 5 },
{ args: '**********', expected: 10 },
{ args: '<<**>>', expected: 2 },
]

describe('Challenge #6: The reindeer on trial', () => {
buildChallengeTestCases({
cases: TEST_CASES,
fn: maxDistance,
})
})
19 changes: 19 additions & 0 deletions 2023/challenge-06/challenge-06.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function maxDistance(movements: string) {
let maxLeft = 0
let maxRight = 0

for (const movement of movements) {
if (movement === '>') {
maxRight++
maxLeft--
} else if (movement === '<') {
maxLeft++
maxRight--
} else {
maxLeft++
maxRight++
}
}

return Math.max(maxLeft, maxRight)
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
| 03 | The naughty elf | 🟢 | [Show](https://adventjs.dev/challenges/2023/3) | [Go](./2023/challenge-03/challenge-03.ts) |
| 04 | Turn the parentheses around | 🟠 | [Show](https://adventjs.dev/challenges/2023/4) | [Go](./2023/challenge-04/challenge-04.ts) |
| 05 | Santa's CyberTruck | 🟠 | [Show](https://adventjs.dev/challenges/2023/5) | [Go](./2023/challenge-05/challenge-05.ts) |
| 06 | -- | -- | -- | -- |
| 06 | The reindeer on trial | 🟢 | [Show](https://adventjs.dev/challenges/2023/6) | [Go](./2023/challenge-06/challenge-06.ts) |
| 07 | -- | -- | -- | -- |
| 08 | -- | -- | -- | -- |
| 09 | -- | -- | -- | -- |
Expand Down

0 comments on commit 20440ba

Please sign in to comment.