-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution2.js
47 lines (43 loc) · 1.2 KB
/
solution2.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
const filePath = require('path').join(__dirname, 'input');
const [n, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function solution (n, arr) {
const calculateScore = (team) => {
let score = 0;
for (let i = 0; i < team.length; i ++) {
for (let j = 0; j < team.length; j ++) {
score += arr[team[i]][team[j]];
}
}
return score;
}
let powerBalance = Infinity;
for (let i = 0; i < (1 << n - 1); i ++) {
const startTeam = [];
const linkTeam = [];
for (let j = 0; j < n; j ++) {
if (i & (1 << j)) {
startTeam.push(j);
} else {
linkTeam.push(j);
}
}
powerBalance = Math.min(powerBalance, Math.abs(calculateScore(startTeam) - calculateScore(linkTeam)));
}
return powerBalance;
}
console.log(solution(+n, arr.map(r => r.split(' ').map(Number))));
function run (input) {
const [n, ...arr] = input
.toString()
.trim()
.split('\n')
.map(r => r.trim())
return solution(+n, arr.map(r => r.split(' ').map(Number)))
}
module.exports = {
run
}