-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
29 lines (23 loc) · 830 Bytes
/
index.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
function distributeWeight(weight) {
const boxLayouts = {
1: [' _ ', '|_|'],
2: [' ___ ', '|___|'],
5: [' _____ ', '| |', '|_____|'],
10: [' _________ ', '| |', '|_________|'],
};
const stackedBoxes = [];
const numbers = Object.keys(boxLayouts).map(Number).reverse();
function findTheBiggestBox(boxWeight) {
return numbers.find((number) => number <= boxWeight);
}
while (weight > 0) {
const boxWeight = findTheBiggestBox(weight);
const [bottom, ...rest] = boxLayouts[boxWeight].slice().reverse();
const last = stackedBoxes.shift();
const newBottom = `${bottom}${last?.slice(bottom.length, -1) ?? ''}`;
stackedBoxes.unshift(...[newBottom, ...rest].reverse());
weight -= boxWeight;
}
return stackedBoxes.join('\n');
}
module.exports = distributeWeight;