SHENZHEN I/O

SHENZHEN I/O

Tesla Challenge 23: Magic Square Generator
3 Comments
Cocoa 22 May, 2023 @ 10:28pm 
I just tried to calculate the whole magic square by the following rules:


A = input
B = input
C = 15 - A - B
E = 5
G = 15 - C - E = 10 - C
I = 15 - A - E = 10 - A
D = 15 - A - G
F = 15 - C - I
H = 10 - B


and got 22 / 146 / 50 .
89o  [author] 9 Mar, 2018 @ 1:03pm 
@pv2b
I used two ROMs to store the eight possible magic squares and read them aloud on the go. :)
pv2b 5 Mar, 2018 @ 2:23pm 
After solving this before reading any of the hints, I feel like I solved this the hard way. My approach:

There is only one 3x3 magic square (and all other magic squares are rotations or reflections). We know first 2 squares (input) and center square (5, never moves).

Knowing that the "magic sum" is always 15 lets us populate a few more numbers. Repeat until we've populated the whole square. This just takes some simple algebra.

After generating a square like this, loop through all digits 1-9, and make sure each digit is present exactly once in the square. This'll tell us that it's the actual magic square. We already know the sums will match, we just need to eliminate square with duplicates or digits outside of [1,9].

Then, just output the square if it's valid, error if not.

Getting all this to fit on the board was not easy. I had to get pretty creative with trace routing!

https://i.imgur.com/YHXSvlp.jpg
https://i.imgur.com/QTB0CuS.jpg
27/1227/65