-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_square.js
73 lines (55 loc) · 1.43 KB
/
magic_square.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Complete the 'formingMagicSquare' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY s as parameter.
*/
function formingMagicSquare(s = []) {
const triple = [
[4, 3, 8],
[8, 1, 6],
[6, 7, 2],
[2, 9, 4],
];
const inverseTriple = [...triple].reverse().map(el => [...el].reverse());
const arr = [
s[0],
[s[0][2], s[1][2], s[2][2]],
[...s[2]].reverse(),
[s[2][0], s[1][0], s[0][0]]
];
// just to be replaced in begin
let lesser = 45;
for (let i = 0; i < 8; i++) {
const combination = i <= 3 ? triple : inverseTriple;
if (i > 0) {
combination.push(combination.shift());
}
const cost = findCost(arr, combination);
if (cost < lesser) {
lesser = cost;
}
}
const midCostReplace = Math.abs(s[1][1] - 5);
return lesser + midCostReplace;
};
function findCost(original, changed) {
let acc = {
cost: 0,
elements: []
};
for (let i = 0; i < 4; i++) {
const originalConj = original[i];
const changedConj = changed[i];
for (let j = 0; j < 3; j++) {
const choosedNumber = changedConj[j];
if (!acc.elements.includes(choosedNumber)) {
acc.cost = acc.cost + Math.abs(originalConj[j] - choosedNumber);
acc.elements.push(choosedNumber);
}
}
}
return acc.cost;
}
const s = [[4, 9, 2], [3, 5, 7], [8, 1, 5]];
console.log(formingMagicSquare(s));