-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (114 loc) · 3.18 KB
/
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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Test input
// var array = [[4, 9, 2],
// [3, 5, 7],
// [8, 1, 5]];
// Correct magic square
// var array = [[4, 9, 2],
// [3, 5, 7],
// [8, 1, 6]];
// VECTOR DE ENTRADA
var user_vector = [4, 9, 2,
3, 3, 7,
8, 1, 7]
var val = 15;
const permutator = (inputArr) => {
let result = [];
const permute = (arr, m = []) => {
if (arr.length === 0) {
result.push(m)
} else {
for (let i = 0; i < arr.length; i++) {
let curr = arr.slice();
let next = curr.splice(i, 1);
permute(curr.slice(), m.concat(next))
}
}
}
permute(inputArr)
return result;
}
function validate_rows(array) {
for (var i = 0; i < array.length; i++) {
var accum = 0;
for (var j = 0; j < array.length; j++) {
accum += array[i][j];
}
if (accum !== val)
return false;
}
return true;
}
function validate_columns(array) {
for (var i = 0; i < array.length; i++) {
var accum = 0;
for (var j = 0; j < array.length; j++) {
accum += array[j][i];
}
if (accum !== val)
return false;
}
return true;
}
function validate_main_diagonal(array) {
var accum = 0;
for (var i = 0; i < array.length; i++) {
accum += array[i][i];
}
if (accum !== val)
return false;
return true;
}
function validate_secondary_diagonal(array) {
var accum = 0;
for (var i = 0; i < array.length; i++) {
for (var j = 2; j > -1; j--) {
if ((i + j) == array.length - 1) {
accum += array[i][j];
}
}
}
if (accum !== val)
return false;
return true;
}
function validate_magic_square(array) {
if (!validate_rows(array) || !validate_columns(array) || !validate_main_diagonal(array) || !validate_secondary_diagonal(array))
return false;
else
return true;
}
function difference (user_vector, vector) {
var sum = 0;
for (var i = 0; i < 9; i++)
sum += Math.abs(user_vector[i] - vector[i]);
return sum;
}
var permutations = permutator([1, 2, 3, 4, 5, 6, 7, 8, 9]);
var minimal_cost = Number.MAX_SAFE_INTEGER; // variable para ir almacenando el costo mínimo
var magic_squares = [];
// Llenamos magic_squares
permutations = permutations.forEach(element => {
var array = [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++){
array[i][j] = element[3 * i + j];
}
}
if(validate_magic_square(array)) {
magic_squares.push(array);
}
});
function convert_magic_squares_to_vectors (magic_squares) {
magic_squares.forEach(element => {
magic_vector = []
// Convertimos cada arreglo 3x3 a un vector donde i representa cada magic square
for (var j = 0; j < element.length; j++) {
for (var k = 0; k < element.length; k++) {
magic_vector.push(element[j][k]);
}
}
minimal_cost = Math.min(minimal_cost, difference(user_vector, magic_vector));
});
return minimal_cost;
};
console.log("El costo mínimo es", convert_magic_squares_to_vectors(magic_squares));