mirror of
https://gitlab.com/mfocko/CodeWars.git
synced 2024-11-09 11:09:07 +01:00
24 lines
466 B
JavaScript
24 lines
466 B
JavaScript
function pick(bags, scale) {
|
|
if (bags.length == 1) {
|
|
return bags[0];
|
|
}
|
|
|
|
let sets = [];
|
|
let sizeOfSet = bags.length / 3;
|
|
|
|
for (let i = 0; i < bags.length; i += sizeOfSet) {
|
|
sets.push(bags.slice(i, i + sizeOfSet));
|
|
}
|
|
|
|
switch (scale.weigh(sets[0], sets[1])) {
|
|
case -1:
|
|
return pick(sets[0], scale);
|
|
break;
|
|
case 0:
|
|
return pick(sets[2], scale);
|
|
break;
|
|
case 1:
|
|
return pick(sets[1], scale);
|
|
break;
|
|
}
|
|
}
|