Skip to content

Commit 5ee798b

Browse files
committed
✨ Challenge #8: Sorting the warehouse
1 parent e18383b commit 5ee798b

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

2023/challenge-08/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Challenge #8: 🏬 Sorting the warehouse
2+
3+
The elves are very busy in Santa Claus' workshop organizing gifts 🎁 for Christmas Eve 🎄.
4+
5+
The input format is special, as it indicates the number of gifts and the type of gift with letters from a to z. For example, '66a11b' means 66 a gifts and 11 b gifts.
6+
7+
The elves have a special system for organizing the gifts:
8+
9+
- Every 10 gifts of the same type are packed in a box, represented by {x}. For example, 20 type a gifts are packed in two boxes like this: {a}{a}.
10+
- Every 5 boxes are stacked on a pallet, represented by [x]. For example, 10 a boxes are stacked on 2 pallets in this way: [a][a].
11+
- Any additional gift is placed in a bag, represented by () and all of them are placed inside. For example, 4 b gifts are placed in a bag like this (bbbb).
12+
13+
The gifts are then placed in the following order: pallets, boxes, and bags. And the gifts appear in the same order as the input string.
14+
15+
Your task is to write a function `organizeGifts` that takes a string of gifts as an argument and returns a string representing the warehouse.
16+
17+
```javascript
18+
const result1 = organizeGifts('76a11b')
19+
console.log(result1)
20+
// `[a]{a}{a}(aaaaaa){b}(b)`
21+
22+
/* Explanation:
23+
24+
76a: 76 gifts type 'a' would be packed in 7 boxes and 6 gifts would be left, resulting in 1 pallet [a] (for the first 5 boxes), 2 loose boxes {a}{a} and a bag with 6 gifts (aaaaaa)
25+
26+
11b: 11 gifts type 'b' would be packed in 1 box and 1 gift would be left, resulting in 1 loose box {b} and a bag with 1 gift (b)
27+
```

2023/challenge-08/solution.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function organizeGifts(gifts) {
2+
let result = "";
3+
let qty = "";
4+
5+
for (let i = 0; i < gifts.length; i++) {
6+
if (isNaN(gifts[i])) {
7+
let number = parseInt(qty);
8+
while (number >= 50) {
9+
const pallets = Math.floor(number / 50);
10+
result += `[${gifts[i]}]`.repeat(pallets);
11+
number -= 50 * pallets;
12+
}
13+
while (number >= 10) {
14+
const boxes = Math.floor(number / 10);
15+
result += `{${gifts[i]}}`.repeat(boxes);
16+
number -= 10 * boxes;
17+
}
18+
if (number >= 1) {
19+
result += `(${gifts[i].repeat(number)})`;
20+
}
21+
qty = "";
22+
} else {
23+
qty += gifts[i];
24+
}
25+
}
26+
return result;
27+
}

2023/challenge-08/solution.spec.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { organizeGifts } from "./solution";
2+
3+
describe("Challenge #8: 🏬 Sorting the warehouse", () => {
4+
const testCases = [
5+
createTestCase(
6+
["76a11b"],
7+
"[a][a][a][a][a][a][a]{a}{a}(aaaaaa){b}(b)",
8+
"should organize gifts as specified in the prompt"
9+
),
10+
createTestCase(
11+
["30a15b10c"],
12+
"[a][a][a][a][a]{a}{a}{a}{a}{a}(aaaaa)[b][b][b][b][b]{b}{b}{b}{b}{b}(bbbbb)[c][c][c][c][c](ccccc)",
13+
"should organize gifts as specified in the prompt"
14+
),
15+
createTestCase(
16+
["5a5b5c5d5e5f5g5h5i5j5k5l5m5n5o5p5q5r5s5t5u5v5w5x5y5z"],
17+
"[a][a][a][a][a]{a}[b][b][b][b][b]{b}[c][c][c][c][c]{c}[d][d][d][d][d]{d}[e][e][e][e][e]{e}[f][f][f][f][f]{f}[g][g][g][g][g]{g}[h][h][h][h][h]{h}[i][i][i][i][i]{i}[j][j][j][j][j]{j}[k][k][k][k][k]{k}[l][l][l][l][l]{l}[m][m][m][m][m]{m}[n][n][n][n][n]{n}[o][o][o][o][o]{o}[p][p][p][p][p]{p}[q][q][q][q][q]{q}[r][r][r][r][r]{r}[s][s][s][s][s]{s}[t][t][t][t][t]{t}[u][u][u][u][u]{u}[v][v][v][v][v]{v}[w][w][w][w][w]{w}[x][x][x][x][x]{x}[y][y][y][y][y]{y}[z][z][z][z][z](zzzzz)",
18+
"should organize gifts as specified in the prompt"
19+
),
20+
];
21+
22+
it.each(testCases)("#$# $description", ({ input, expected, description }) => {
23+
expect(organizeGifts(...input)).toEqual(expected);
24+
});
25+
});
26+
27+
function createTestCase(input, expected, description) {
28+
return { input, expected, description };
29+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
104104
| 05 | [Santa's CyberTruck](2023/challenge-05) | 🟠 | [Show](2023/challenge-05/solution.js) |
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) |
107+
| 08 | [Sorting the warehouse](2023/challenge-08) | 🟠 | [Show](2023/challenge-08/solution.js) |
107108

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

0 commit comments

Comments
 (0)