AdventJS 2023: Day 6 Challenge
Tags: javascript, webdev, challenge, node
Published: 2023-12-07 20:02:56

---
title: AdventJS 2023: Day 6 Challenge
published: true
date: 2023-12-06 06:00:00 UTC
tags: javascript, webdev, challenge, node
canonical_url: https://alexvalle.dev/adventjs-2023-day-6-challenge
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmcq2zltn4skqm0od4h5.png"
---

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

[Solution to the previous challenge](https://dev.to/fenriuz/adventjs-2023-day-5-challenge-2o42)

[Solution to the next challenge](https://dev.to/fenriuz/adventjs-2023-day-7-challenge-3hak)

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

- [**Challenge Description**](#challenge-description)
- [**Analysis**](#analysis)
   * [**Inputs**](#inputs)
   * [**Output**](#output)
   * [**Considerations**](#considerations)
- [**Solution**](#solution)
   * [**Code**](#code)
   * [**Community Solutions**](#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 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:

## **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!