# AdventJS 2023: Day 6 Challenge

Solution to the [Challenge #6](https://adventjs.dev/challenges/2023/6) of [AdventJS 2023](https://adventjs.dev)

[Solution to the](https://dev.to/fenriuz/adventjs-2023-reto-del-dia-5-1dpk) [Solution to the next challenge](https://alexvalle.dev/adventjs-2023-day-5-challenge)[previous challenge](https://dev.to/fenriuz/adventjs-2023-reto-del-dia-5-1dpk)

[Solution to the next challenge](https://alexvalle.dev/adventjs-2023-day-7-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**:

```javascript
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](https://adventjs.dev/es/challenges/2023/6) of [AdventJS 2023](https://adventjs.dev/es) [Solution to the previous challenge](https://dev.to/fenriuz/adventjs-2023-reto-del-dia-5-1dpk) [Solution to the next challenge](https://dev.to/fenriuz)

{%- # TOC start (generated with [https://github.com/derlin/bitdowntoc](https://adventjs.dev/es/challenges/2023/6)) -%}

* [Challenge Description](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#challenge-description)
    
* [Analysis](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#analysis)
    
    * [Inputs](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#inputs)
        
    * [Output](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#output)
        
    * [Considerations](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#considerations)
        
* [Solution](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#solution)
    
    * [Code](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#code)
        
    * [Community Solutions](https://chat.openai.com/c/d961ba32-c0d2-49b6-91d3-1a2d0bf8054b#community-solutions)
        

{%- # 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 &gt;&gt;*&lt;, 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:

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

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

```javascript
/**
 * 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](https://github.com/cristianstu):

```javascript
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](https://github.com/jfes29):

```javascript
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!
