# AdventJS 2023: Day 6 Challenge

## Calculating the Maximum Distance that the Reindeer Can Travel

Solution to the Challenge #6 of AdventJS 2023

Solution to the Solution to the next challengeprevious challenge

Solution to the next challenge

## Challenge Description

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**:

```
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**.

Here's the translation of your blog from Spanish to English, leaving the code and links untouched:

Solution to the Challenge #6 of AdventJS 2023 Solution to the previous challenge Solution to the next challenge

{%- # TOC start (generated with https://github.com/derlin/bitdowntoc) -%}

{%- # TOC end -%}

**Challenge Description**

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

For this, they have a string of text 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 can go right or left (whichever maximizes the final traveled distance), and then goes to the left.

The elves want to know what is the maximum distance the reindeer travels at the end of all movements.

In the example above, the maximum distance the reindeer travels is 2. It goes to the right two times +2, then with the * can go to the right again to maximize the distance +1, and then goes to the left -1.

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

```
javascriptCopy codeconst 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 if it's to the left or right, the distance is the absolute value of the maximum traveled distance at the end of the movements.

**Analysis**

The goal is to find the maximum distance that the reindeers can travel. It doesn't matter if they travel it to the left or right, the point is to find the maximum distance.

**Inputs**

- Movements (
`movements`

): A string with the movements, where each character represents the direction of the reindeer's movement.

**Output**

- The number of the maximum distance that can be reached with those movements

**Considerations**

- The maximum distance must be found regardless of whether the movement is to the right or left.

**Solution**

Solving this exercise is not that complicated and, as always, we have many paths and alternatives. A simple approach is to count how many times each movement is repeated and then do the final sum to find the result.

**Code**

```
/**
* Calculates the maximum distance based on the given movements.
*
* @param {string} movements - A string representing the movements.
* @returns {number} The maximum distance.
*/
function maxDistance(movements) {
// Initialize the directions object with default values
const directions = {
"<": 0, // Represents left movement
">": 0, // Represents right movement
"*": 0, // Represents movement in any direction
};
// Count the occurrences of each movement
for (const movement of movements) {
directions[movement] += 1;
}
// Calculate the maximum distance
// Math.abs() returns the absolute value of a number
return Math.abs(directions["<"] - directions[">"]) + directions["*"];
}
```

**Community Solutions**

Solution by cristianstu:

```
function maxDistance(movements) {
const a = movements.split(/>/g).length - 1
const b = movements.split(/</g).length - 1
const c = movements.length - a - b;
return Math.abs(a - b) + c
}
```

Solution by jfes29:

```
const maxDistance(movements) {
let movements1 = movements.replaceAll("*", "");
let movements2 = movements1.replaceAll("<", "");
let movements3 = movements1.replaceAll(">", "");
return movements.length - 2 * Math.min(movements2.length, movements3.length);
}
```

And that was the challenge for December 6th and its solutions. Do you have another alternative solution? Leave it in the comments!