Solution to the Challenge #6 of AdventJS 2023
Solution to the previous 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:
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!
Top comments (5)
function maxDistance(movements) {
let distance = 0, asterisks = 0;
for (let character of movements) {
if (character == ">")
distance++;
else if (character == "<")
distance--;
else
asterisks++;
}
if (distance < 0)
distance *= -1;
return distance + asterisks;
}
Hi Alejandro! Great solution! This line
if (distance < 0) distance *= -1;
is the key, clever. Thanks for sharing!Hi, Thanks, at the beginning I used Math.Abs but I finally changed by distance *= -1 because the solution was scored better without Math library, I dont know why :)
Sometimes it's because performance increased or cognitive complexity decreased with the change. 🚀
Or sometimes the server performs better and gives you a better score. 🎲
yes, you´re right