-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
84 lines (80 loc) · 1.88 KB
/
Copy pathscript.js
File metadata and controls
84 lines (80 loc) · 1.88 KB
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
// Function thats rotate 2d matrix by 90 degrees
function rotate2d (matrix){
let emptyMatrix = [
[0,0,0],
[0,0,0],
[0,0,0]
]
for (let c = 0 ; c < 3 ; c++){
for(let r = 0 ; r < 3 ; r++){
emptyMatrix[2-r][c] = matrix[c][r]
}
}
return emptyMatrix
}
function mirorMatrix (matrix){
let emptyMatrix = [
[0,0,0],
[0,0,0],
[0,0,0]
]
for (let c = 0 ; c < 3 ; c++){
for(let r = 0 ; r < 3 ; r++){
emptyMatrix[c][2-r] = matrix[c][r]
}
}
return emptyMatrix
}
function formingMagicSquare(s) {
// Write your code here
let
equivilant =[[
[8,3,4],
[1,5,9],
[6,7,2]
]],
mirrored = [],
ways = [],
main = s;
for(let i = 0 ; i < 3 ; i++){
equivilant.push(rotate2d(equivilant[i]))
}
mirrored.push(mirorMatrix(equivilant[0]));
for(let i = 0 ; i < 3 ; i++){
let there = rotate2d(mirrored[i])
mirrored.push(there)
console.log(there)
};
equivilant.push(...mirrored)
// to Print the matrix :
// let final = equivilant[0];
// let string = ""
// for(let i in final){
// let row = ""
// for (let r in final[0]){
// row += `${final[i][r]} `
// }
// row += "\n"
// string += row
// }
// return string
// ________________
// ________________
// ________________
// now we start comparing all maticies with the given one
for(let i in equivilant){
let errors = 0;
for(let r in s){
for(let c in s){
if(equivilant[i][c][r] != s[r][c]){errors++}
}
}
ways.push(errors)
}
return Math.min(...ways)
}
console.log(formingMagicSquare([
[8,3,4],
[3,2,9],
[6,7,2]
]))