-
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 #06
- Loading branch information
Showing
4 changed files
with
70 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,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.** |
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,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, | ||
}) | ||
}) |
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,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) | ||
} |
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