Created
July 19, 2023 15:09
-
-
Save utlime/6ebda6323cd91eb830f6270a2a798405 to your computer and use it in GitHub Desktop.
sort battery by capacity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseData(data) { | |
return data | |
.split("\n") | |
.filter(Boolean) | |
.map(function (row) { return Number.parseInt(row); }) | |
.map(function (capacity, index) { return ({ id: index + 1, capacity: capacity }); }); | |
} | |
function getTotalCapacity(batteries) { | |
return batteries | |
.map(function (_a) { | |
var capacity = _a.capacity; | |
return capacity; | |
}) | |
.reduce(function (acc, cur) { return acc + cur; }, 0); | |
} | |
function sortByCapacity(batteries) { | |
return batteries.sort(function (a, b) { return b.capacity - a.capacity; }); | |
} | |
function clone(batteries) { | |
return batteries.map(function (bat) { return ({ id: bat.id, capacity: bat.capacity }); }); | |
} | |
function groupBySort(batteries, s) { | |
return sortByCapacity(clone(batteries)).reduce(function (acc, cur) { | |
if (acc.length < s) { | |
acc.push([cur]); | |
} | |
else { | |
var groupIndex = sortByCapacity(acc.map(function (group, id) { return ({ | |
id: id, | |
capacity: getTotalCapacity(group), | |
}); }))[s - 1].id; | |
acc[groupIndex].push(cur); | |
} | |
return acc; | |
}, []); | |
} | |
function resortGrouped(groups, s, p) { | |
var total = getTotalCapacity(groups.flat()); | |
var totalPerLine = total / s; | |
var result = groups.map(clone); | |
var groupsToBalance; | |
for (var n = 0; n < 100; n++) { | |
groupsToBalance = result.map(function (group) { return getTotalCapacity(group) - totalPerLine; }); | |
console.log(groupsToBalance); | |
if (groupsToBalance.every(function (v) { return v === 0; })) { | |
return result; | |
} | |
for (var i = 0; i < s; i++) { | |
if (groupsToBalance[i] >= 0) { | |
continue; | |
} | |
for (var j = 0; j < s; j++) { | |
if (groupsToBalance[j] <= 0) { | |
continue; | |
} | |
for (var b_i = 0; b_i < p; b_i++) { | |
for (var b_j = 0; b_j < p; b_j++) { | |
var diff = result[j][b_j].capacity - result[i][b_i].capacity; | |
if (diff > 0 && diff <= Math.abs(groupsToBalance[i])) { | |
var buffer = result[j][b_j]; | |
result[j][b_j] = result[i][b_i]; | |
result[i][b_i] = buffer; | |
b_j = p; | |
b_i = p; | |
j = s; | |
i = s; | |
} | |
} | |
} | |
} | |
} | |
} | |
return result; | |
} | |
function group(options) { | |
var batteries = parseData(options.data); | |
var result = groupBySort(batteries, options.s); | |
result = resortGrouped(result, options.s, options.p); | |
result = result.map(sortByCapacity); | |
return result; | |
} | |
(function main() { | |
var res = group({ | |
data: "\n2690\n2710\n2690\n2710\n2700\n2680\n2700\n2690\n2680\n2690\n2720\n2670\n2740\n2690\n2700\n2700\n2750\n2730\n2700\n2720\n2610\n2700\n2750\n2780\n2760\n2740\n2790\n2750\n2700\n2740\n2700\n2740\n2750\n2700\n2710\n2760\n2840\n2710\n2840\n2710\n", | |
p: 10, | |
s: 4, | |
}); | |
console.log(res); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Battery = { id: number; capacity: number }; | |
function parseData(data: string): Array<Battery> { | |
return data | |
.split("\n") | |
.filter(Boolean) | |
.map((row) => Number.parseInt(row)) | |
.map((capacity, index) => ({ id: index + 1, capacity })); | |
} | |
function getTotalCapacity(batteries: Array<Battery>): number { | |
return batteries | |
.map(({ capacity }) => capacity) | |
.reduce((acc, cur) => acc + cur, 0); | |
} | |
function sortByCapacity(batteries: Array<Battery>): Array<Battery> { | |
return batteries.sort((a, b) => b.capacity - a.capacity); | |
} | |
function clone(batteries: Array<Battery>): Array<Battery> { | |
return batteries.map((bat) => ({ id: bat.id, capacity: bat.capacity })); | |
} | |
function groupBySort( | |
batteries: Array<Battery>, | |
s: number, | |
): Array<Array<Battery>> { | |
return sortByCapacity(clone(batteries)).reduce((acc, cur) => { | |
if (acc.length < s) { | |
acc.push([cur]); | |
} else { | |
const groupIndex = sortByCapacity( | |
acc.map((group, id) => ({ | |
id, | |
capacity: getTotalCapacity(group), | |
})), | |
)[s - 1].id; | |
acc[groupIndex].push(cur); | |
} | |
return acc; | |
}, [] as Battery[][]); | |
} | |
function resortGrouped( | |
groups: Array<Array<Battery>>, | |
s: number, | |
p: number, | |
): Array<Array<Battery>> { | |
const total = getTotalCapacity(groups.flat()); | |
const totalPerLine = total / s; | |
const result = groups.map(clone); | |
let groupsToBalance; | |
for (let n = 0; n < 100; n++) { | |
groupsToBalance = result.map( | |
(group) => getTotalCapacity(group) - totalPerLine, | |
); | |
console.log(groupsToBalance); | |
if (groupsToBalance.every((v) => v === 0)) { | |
return result; | |
} | |
for (let i = 0; i < s; i++) { | |
if (groupsToBalance[i] >= 0) { | |
continue; | |
} | |
for (let j = 0; j < s; j++) { | |
if (groupsToBalance[j] <= 0) { | |
continue; | |
} | |
for (let b_i = 0; b_i < p; b_i++) { | |
for (let b_j = 0; b_j < p; b_j++) { | |
const diff = result[j][b_j].capacity - result[i][b_i].capacity; | |
if (diff > 0 && diff <= Math.abs(groupsToBalance[i])) { | |
const buffer = result[j][b_j]; | |
result[j][b_j] = result[i][b_i]; | |
result[i][b_i] = buffer; | |
b_j = p; | |
b_i = p; | |
j = s; | |
i = s; | |
} | |
} | |
} | |
} | |
} | |
} | |
return result; | |
} | |
function group(options: { | |
data: string; | |
p: number; | |
s: number; | |
}): Array<Array<Battery>> { | |
const batteries = parseData(options.data); | |
let result = groupBySort(batteries, options.s); | |
result = resortGrouped(result, options.s, options.p); | |
result = result.map(sortByCapacity); | |
return result; | |
} | |
(function main() { | |
const res = group({ | |
data: ` | |
2690 | |
2710 | |
2690 | |
2710 | |
2700 | |
2680 | |
2700 | |
2690 | |
2680 | |
2690 | |
2720 | |
2670 | |
2740 | |
2690 | |
2700 | |
2700 | |
2750 | |
2730 | |
2700 | |
2720 | |
2610 | |
2700 | |
2750 | |
2780 | |
2760 | |
2740 | |
2790 | |
2750 | |
2700 | |
2740 | |
2700 | |
2740 | |
2750 | |
2700 | |
2710 | |
2760 | |
2840 | |
2710 | |
2840 | |
2710 | |
`, | |
p: 10, | |
s: 4, | |
}); | |
console.log(res); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment