SHENZHEN I/O

SHENZHEN I/O

Not enough ratings
Tesla Challenge 23: Magic Square Generator
   
Award
Favorite
Favorited
Unfavorite
File Size
Posted
2.019 KB
8 Apr, 2017 @ 11:25am
1 Change Note ( view )

Subscribe to download
Tesla Challenge 23: Magic Square Generator

In 1 collection by 89o
SHENZHEN I/O Tesla Challenge Pack
24 items
Description
Subject: Magic squares
From: Carl Tesky <carl@longteng-co-ltd.gd.cn>

You know how the I Ching was totally 'in' just 2 months ago? Well, the thing about trends is that, yes, they don't last. Unfortunately our little oracles we made don't sell anymore, because now everyone's all about magic squares. You know the story of the Pen Pineapple Apple Pen meme back in 2016? Same thing. Now we'll need to make a magic square generator.
For those of you who don't know: A magic square is a square where all rows and columns, and both diagonals, add up to the same number. There is only one possible 3x3 magic square, not counting rotations and reflections, and here it is:
4 9 2
3 5 7
8 1 6
I've created an LCD display that works for these things. Now we'll just need the generator itself.

---------

Small hint: It's up to you if you calculate the magic square 'on the go' or if you read it from ROM storage (I did the latter)

Big hint: Before creating the square, check if the input creates one of the 8 possible magic squares.

Optimization tip: Since all numbers in the magic square are 1-digit, it's possible to use dgt and dst to compress all 8 possible squares into just two ROMs.
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