Skip to content

Commit 2f39361

Browse files
committed
✨ Challenge #9: Switch the lights
1 parent 5ee798b commit 2f39361

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

2023/challenge-09/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Challenge #9: 🚦 Switch the lights
2+
3+
The city is illuminating with Christmas lights 🎄, and as every year, they need to be fixed!
4+
5+
The lights come in two colors: 🔴 and 🟢. For the display 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, and so on.
6+
7+
We have been asked to write a function `adjustLights` that, given an array of strings with the color of each light, returns the minimum number of lights that need to be changed for the colors to alternate.
8+
9+
```javascript
10+
adjustLights(['🟢', '🔴', '🟢', '🟢', '🟢'])
11+
// -> 1 (change the fourth light to 🔴)
12+
13+
adjustLights(['🔴', '🔴', '🟢', '🟢', '🔴'])
14+
// -> 2 (change the second light to 🟢 and the third to 🔴)
15+
16+
adjustLights(['🟢', '🔴', '🟢', '🔴', '🟢'])
17+
// -> 0 (they are already alternating)
18+
19+
adjustLights(['🔴', '🔴', '🔴'])
20+
// -> 1 (change the second light to 🟢)
21+
```

2023/challenge-09/solution.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function adjustLights(lights) {
2+
const result = lights.reduce(
3+
(res, currentLight) => {
4+
const comparison = +(currentLight == res.prevLight);
5+
return {
6+
res: res.res + comparison,
7+
prevLight: [currentLight, " "][comparison],
8+
};
9+
},
10+
{ res: 0, prevLight: "" }
11+
).res;
12+
13+
return result;
14+
}

2023/challenge-09/solution.spec.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { adjustLights } from "./solution";
2+
3+
describe("Challenge #9: 🚦 Switch the lights", () => {
4+
const testCases = [
5+
createTestCase(
6+
[["🟢", "🔴", "🟢", "🟢", "🟢"]],
7+
1,
8+
"should return 1 (change the fourth light to 🔴)"
9+
),
10+
createTestCase(
11+
[["🔴", "🔴", "🟢", "🟢", "🔴"]],
12+
2,
13+
"should return 2 (change the second light to 🟢 and the third to 🔴)"
14+
),
15+
createTestCase(
16+
[["🟢", "🔴", "🟢", "🔴", "🟢"]],
17+
0,
18+
"should return 0 (they are already alternating)"
19+
),
20+
createTestCase(
21+
[["🔴", "🔴", "🔴"]],
22+
1,
23+
"should return 1 (change the second light to 🟢)"
24+
),
25+
];
26+
27+
it.each(testCases)("#$# $description", ({ input, expected, description }) => {
28+
expect(adjustLights(...input)).toEqual(expected);
29+
});
30+
});
31+
32+
function createTestCase(input, expected, description) {
33+
return { input, expected, description };
34+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
105105
| 06 | [The reindeer on trial](2023/challenge-06) | 🟢 | [Show](2023/challenge-06/solution.js) |
106106
| 07 | [The 3D boxes](2023/challenge-07) | 🟢 | [Show](2023/challenge-07/solution.js) |
107107
| 08 | [Sorting the warehouse](2023/challenge-08) | 🟠 | [Show](2023/challenge-08/solution.js) |
108+
| 09 | [Switch the lights](2023/challenge-09) | 🟢 | [Show](2023/challenge-09/solution.js) |
108109

109110
[^1]: **Difficulty**: 🟢 Easy 🟠 Medium 🔴 Hard 🟣 Very Hard
110111

0 commit comments

Comments
 (0)