-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge8.js
58 lines (42 loc) · 1.63 KB
/
challenge8.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Points: 210
OPS/S : 2356
Cognitive complexity: 9
*/
function organizeGifts(gifts) {
const giftsToMap = () => {
let valuesMap = new Map();
let slices = gifts.match(/[a-z]+|\d+/gi);
for (let i = 0; i < slices.length; i += 2) {
let letter = slices[i + 1];
let count = Number(slices[i]);
valuesMap.set(letter, count)
}
return valuesMap;
}
const giftsMap = giftsToMap();
return [...giftsMap].reduce((acc, [key, value]) => {
let remainingGiftsCount = value;
while (remainingGiftsCount > 0) {
if (remainingGiftsCount >= 50) {
const palletsCount = Math.floor(remainingGiftsCount / 50);
acc += `[${key}]`.repeat(palletsCount);
remainingGiftsCount %= palletsCount * 50;
} else if (remainingGiftsCount >= 10) {
const boxCount = Math.floor(remainingGiftsCount / 10);
acc += `{${key}}`.repeat(boxCount);
remainingGiftsCount %= boxCount * 10;
} else {
acc += '(' + `${key}`.repeat(remainingGiftsCount) + ')';
remainingGiftsCount %= 1;
}
}
return acc;
}, '');
}
const result = organizeGifts('76a11b');
console.log(result);
/* Explanation:
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)
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)
*/