Magic Square Coding Challenge

The Problem:

5 3 4
1 5 8
6 4 2
8 3 4
1 5 9
6 7 2
  • int s[3][3]: a 3x3 array of integers
  • int: the minimal total cost of converting the input square to a magic square

The Solution:

function formingMagicSquare(s) {
// Length of array
let n = s[0].length
let newArr = []
// iterate through matrix, add each element to newArr
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
newArr.push(s[i][j])
}
}
// print all corner numbers (index: 0,2,6,8) (values: 2-8,4-6,8-2,6-4)

// candidate pairs
// (1,9)(9,1)
// (3,7)(7,3)
// (2,8)(8,2)
// (4,6)(6,4)
let changes = []
let result = 0
for (let i = 0; i < newArr.length; i++) {
// NOTE: Work on writing a dynamic code block
switch (i) {
case 0: {
if (newArr[i] == 8) {
let beforeChange = newArr[8]
newArr[8] = 2
changes.push(Math.abs(beforeChange - newArr[8])) }
if (newArr[i] == 2) {
let beforeChange = newArr[8]
newArr[8] = 8
changes.push(Math.abs(beforeChange - newArr[8])) }
if (newArr[i] == 4) {
let beforeChange = newArr[8]
newArr[8] = 6
changes.push(Math.abs(beforeChange - newArr[8])) }
if (newArr[i] == 6) {
let beforeChange = newArr[8]
newArr[8] = 4
changes.push(Math.abs(beforeChange - newArr[8]))
}
}
break;
case 1:
if (newArr[i] == 1) {
let beforeChange = newArr[7]
newArr[7] = 9
changes.push(Math.abs(beforeChange - newArr[7]))
}
if (newArr[i] == 9) {
let beforeChange = newArr[7]
newArr[7] = 1
changes.push(Math.abs(beforeChange - newArr[7]))
}
if (newArr[i] == 3) {
let beforeChange = newArr[7]
newArr[7] = 7
changes.push(Math.abs(beforeChange - newArr[7]))
}
if (newArr[i] == 7) {
let beforeChange = newArr[7]
newArr[7] = 3
changes.push(Math.abs(beforeChange - newArr[7]))
}
break;
case 2: {
if (newArr[i] == 8) {
let beforeChange = newArr[6]
newArr[6] = 2
changes.push(Math.abs(beforeChange - newArr[6])) }
if (newArr[i] == 2) {
let beforeChange = newArr[6]
newArr[6] = 8
changes.push(Math.abs(beforeChange - newArr[6])) }
if (newArr[i] == 4) {
let beforeChange = newArr[6]
newArr[6] = 6
changes.push(Math.abs(beforeChange - newArr[6])) }
if (newArr[i] == 6) {
let beforeChange = newArr[6]
newArr[6] = 4
changes.push(Math.abs(beforeChange - newArr[6]))
} }
break;
case 3: {
if (newArr[i] == 1) {
let beforeChange = newArr[5]
newArr[5] = 9
changes.push(Math.abs(beforeChange - newArr[5]))
}
if (newArr[i] == 9) {
let beforeChange = newArr[5]
newArr[5] = 1
changes.push(Math.abs(beforeChange - newArr[5]))
}
if (newArr[i] == 3) {
let beforeChange = newArr[5]
newArr[5] = 7
changes.push(Math.abs(beforeChange - newArr[5]))
}
if (newArr[i] == 7) {
let beforeChange = newArr[5]
newArr[5] = 3
changes.push(Math.abs(beforeChange - newArr[5]))
}
break;
}
case 4: {
let beforeChange = newArr[i]
newArr[i] = 5
changes.push(Math.abs(beforeChange - newArr[i]))
break;
}
case 5: {
if (newArr[i] == 1) {
let beforeChange = newArr[3]
newArr[3] = 9
changes.push(Math.abs(beforeChange - newArr[3]))
}
if (newArr[i] == 9) {
let beforeChange = newArr[3]
newArr[3] = 1
changes.push(Math.abs(beforeChange - newArr[3]))
}
if (newArr[i] == 3) {
let beforeChange = newArr[3]
newArr[3] = 7
changes.push(Math.abs(beforeChange - newArr[3]))
}
if (newArr[i] == 7) {
let beforeChange = newArr[3]
newArr[3] = 3
changes.push(Math.abs(beforeChange - newArr[3]))
}
break;
}
case 6: {
if (newArr[i] == 8) {
let beforeChange = newArr[2]
newArr[2] = 2
changes.push(Math.abs(beforeChange - newArr[2])) }
if (newArr[i] == 2) {
let beforeChange = newArr[2]
newArr[2] = 8
changes.push(Math.abs(beforeChange - newArr[2])) }
if (newArr[i] == 4) {
let beforeChange = newArr[2]
newArr[2] = 6
changes.push(Math.abs(beforeChange - newArr[2])) }
if (newArr[i] == 6) {
let beforeChange = newArr[2]
newArr[2] = 4
changes.push(Math.abs(beforeChange - newArr[2]))
} }
break;
case 7:
if (newArr[i] == 1) {
let beforeChange = newArr[1]
newArr[1] = 9
changes.push(Math.abs(beforeChange - newArr[1]))
}
if (newArr[i] == 9) {
let beforeChange = newArr[1]
newArr[1] = 1
changes.push(Math.abs(beforeChange - newArr[1]))
}
if (newArr[i] == 3) {
let beforeChange = newArr[1]
newArr[1] = 7
changes.push(Math.abs(beforeChange - newArr[1]))
}
if (newArr[i] == 7) {
let beforeChange = newArr[1]
newArr[1] = 3
changes.push(Math.abs(beforeChange - newArr[1]))
}
break;
case 8: {
if (newArr[i] == 8) {
let beforeChange = newArr[0]
newArr[0] = 2
changes.push(Math.abs(beforeChange - newArr[0])) }
if (newArr[i] == 2) {
let beforeChange = newArr[0]
newArr[0] = 8
changes.push(Math.abs(beforeChange - newArr[0])) }
if (newArr[i] == 4) {
let beforeChange = newArr[0]
newArr[0] = 6
changes.push(Math.abs(beforeChange - newArr[0])) }
if (newArr[i] == 6) {
let beforeChange = newArr[0]
newArr[0] = 4
changes.push(Math.abs(beforeChange - newArr[0])) } }
break;
default:
} result = changes.reduce((acc, cur) => {
return acc + cur
})
}
console.log("Result: ", result)
return result
}

Thought Process:

  • 3 x 3 magic square needs to sum up to 15.
  • 4 corners of the square need to be even numbers
  • Middle square is going to always be a 5
  • Numbers surrounding the 5 thats are not corners need to be odd numbers
  • Diagonal corner numbers have the be either [2 and 8] or [4 and 6] because they have to add up to 10 since the sum of the diagonal should be 10 + 5(since 5 is always in the middle)
  • Odd diagonals also have to add up to 10 so the pairs for those are [7 and 3] or [9 and 1]
  1. We got the length of one array in the 2D array.
  2. Iterated through the dynamic array and push the values to a new array
  3. Then we declared a new empty array and a counter that started at 0.
  4. Now we iterate for each number in the new array to check is they are following the previous rules mentioned. (This works because the numbers were pushed in order.)
  5. We check each number with a switch case. While keeping track of previous numbers and newly changed numbers.
  6. We then subtract the previous change number and the new number to get the difference. And push the difference to the changes array.
  7. We later sum all of the numbers in the changes array resulting in the total cost. Which we save in the results variable.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to Insert & Select data using SQLite in a C# console application

PRTG Network Monitoring

Best practice scraping websites using pagination with indeed.com case studies

Misreading RSS feeds

Machine Learning 101 All Algorithms in python (Logistic Regression)

Why you need to GIT good

4 branching workflows for Git

Jenkins with Dockerhub integration

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nerly Ton

Nerly Ton

More from Medium

Google Authenticator export format

AI Software Developer Experience at IPrep Learning Solutions (Cerebry) Week 20

AEM SDK Dispatcher Validation Using Github Action (Experimental)

StackTribe T-Shirt is Now Available!