
I am a Software Developer who loves coding, cats and sharing content!
Search for a command to run...

I am a Software Developer who loves coding, cats and sharing content!
No comments yet. Be the first to comment.
AdventJS is a serie of coding challenges that are released every day since december 1st to december 25th (Christmas). The idea es solving each of one to practice and improve our coding skills.
Palindrome Swap Challenge: Finding the indexes!
A React Native alternative

Palindrome Swap Challenge: Finding the indexes!

Drawing Christmas Trees!

Organizing Gifts for Christmas Eve!

The solution to the Challenge #9 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
They are turning on the Christmas lights π in the city and, as every year, they have to be fixed!
The lights are of two colors: π΄ and π’ . For the effect to be appropriate, they must always alternate. That is, if the first light is red, the second must be green, the third red, the fourth green, etc.
We have been asked to write a function adjustLights that, given an array of strings with the color of each light, return the minimum number of lights that need to be changed for the colors to alternate.
adjustLights(['π’', 'π΄', 'π’', 'π’', 'π’'])
// -> 1 (you change the fourth light to π΄)
adjustLights(['π΄', 'π΄', 'π’', 'π’', 'π΄'])
// -> 2 (you change the second light to π’ and the third to π΄)
adjustLights(['π’', 'π΄', 'π’', 'π΄', 'π’'])
// -> 0 (they are already alternating)
adjustLights(['π΄', 'π΄', 'π΄'])
// -> 1 (you change the second light to π’)
For this exercise, the goal is to find the least number of changes necessary for the lights to alternate. The complexity lies not so much in leaving them alternating, but in finding the fewest number of changes to get to that point.
lights): An array of strings where each color of light goes.["π’", "π΄", "π΄", "π’", "π΄", "π’", "π΄", "π’", "π΄"]. We start with a green, then a red and subsequently another red, if we change that red to green we realize that the next one is also green and we must also change it, in this case to a red, but then another red would follow... and so on, resulting in a 7. The point is that if we simply change the first green to a red and the first red to a green, we would have everything solved with only 2 changes.Considering that the colors must alternate, we can reason that if we arrange the array in such a way, it will start with either the red or green color and alternate from there, so we can evaluate both scenarios and count how many changes would be needed, thus mitigating the problem mentioned in the considerations.
/**
* Organizes alternately the lights in an array.
*
* @param {string[]} lights - The array of lights.
* @return {number} Number of changes needed to organize the lights.
*/
function adjustLights(lights) {
// Initialize variables to count the necessary changes
// both if starting by red or green
let redCount = 0;
let greenCount = 0;
// Iterate over each light color in the array
lights.forEach((light, index) => {
// Check if the index is even or odd
// this in order to count the changes
// for both cases, red even and green odd
// or green even and red odd
if (index % 2) {
// We will evaluate the even cases
// If it's green, add +1 to green
if (light === "π’") greenCount++;
// If it's red, add +1 to red
if (light === "π΄") redCount++;
} else {
// Otherwise for the odd ones
// If the light is red, we do the opposite and +1 for green
if (light === "π΄") greenCount++;
// If the light is green, +1 for red
if (light === "π’") redCount++;
}
});
// Return the minimum between red and green
return Math.min(redCount, greenCount);
}
Solution by Jioh19:
function adjustLights(lights) {
const color = ['π’', 'π΄'];
let res = 0;
for (const [i, light] of lights.entries()) {
res += +(light == color[i % 2]);
}
return Math.min(res, lights.length - res);
}
Solution by Yasai:
function adjustLights(lights) {
const init = lights[0];
let rev = false;
let count1 = 0;
let count2 = 0;
for (const light of lights) {
count1 += rev == (light == init);
rev = !rev;
}
rev = true;
for (const light of lights) {
count2 += rev == (light == init);
rev = !rev;
}
count1 = Math.min(count1, count2);
// Code here
return count1;
}
And that was the challenge for December 9th and its solutions. Give a like if you liked the challenge or the solution!
Do you have another alternative solution? Leave it in the comments!