31
4
In math a magic square is an N×N grid of numbers from 1 to N2 such that every row, column, and diagonal sums to the same total. For example here's a 3×3 magic square:
In this challenge we'll extend the idea to magic code squares where, instead of numbers, each grid cell is any non-newline character. They can be be repeated and in any order.
And instead of sums, each row, column, and diagonal is viewed as an N-character single-line program whose output is a distinct number from 0 to 2N+1 (inclusive).
For example, if you had a 4×4 magic code square that looked like
ABCD
EFGH
IJKL
MNOP
it would need to output the numbers 0 through 9, perhaps like:
ABCD-9
EFGH-2
IJKL-6
MNOP-5
/||||\
3 4180 7
That is, the ten embedded 4-character single-line programs that form the rows, columns, and diagonals of the grid each need to output a unique number from 0 through 9. In this case:
ABCD -> 9
EFGH -> 2
IJKL -> 6
MNOP -> 5
AFKP -> 7
DHLP -> 0
CGKO -> 8
BFJN -> 1
AEIM -> 4
DGJM -> 3
(Note that the diagonals are read from top to bottom. This is required.)
As another example, this 3×3 code square
d*P
0##
! d
would need to output 0 through 7 when its rows, column, and diagonals are run to make it magical, perhaps like:
d*P-1
0##-0
! d-6
/|||\
5 432 7
Scoring
The goal of this challenge is not necessarily to make the smallest or the biggest magic code square. In some programming languages very small magic code squares might be easy to make and in other languages very large ones might be.
The challenge here is to create what you think is the best, most elegant, or most impressive magic code square or related set of magic code squares. Hence this is a popularity-contest, the answer with the highest number of votes wins.
Your submission must include at least one valid magic code square, and may include many related ones if you have found them, or even an infinite neighborhood of them.
Please list the N (or range of N's) you found at the top of your answer.
For submitting multiple magic code squares in the same language but with very different implementations use multiple answers.
Of course you are encouraged to share your process of searching for magic code squares and explain why yours is interesting! (Info about any Parker magic code squares might be funny.)
Is there a minimum grid size? – Lyxal – 2020-01-22T21:44:31.440
1@Lyxal The effective minimum is 2x2 since 1x1 is impossible. – Calvin's Hobbies – 2020-01-22T21:51:49.777
Are we allowed to exit with an error? – Lyxal – 2020-01-22T22:05:04.397
1
@Lyxal I'll say yes, per the meta default - "terminating with an error or an uncaught exception is fine here, as long as it doesn't produce stray output to STDOUT"
– Calvin's Hobbies – 2020-01-22T22:09:45.0602@Calvin'sHobbies We missed you! – Luis Mendo – 2020-01-22T22:34:59.147
the diagonal top-right -> bottom-left seems to be reverse to me, is there a reason it's not bottom-left -> top-right? – Kaddath – 2020-01-23T10:02:18.417
2
@Kaddath As far as I know an anti-diagonal usually goes from the top-right to bottom-left. To quote Wikipedia: "The antidiagonal of a dimension N square matrix ..., runs from the top right corner to the bottom left corner." Languages with a builtin of the anti-diagonal(s) of a matrix (like Jelly or 05AB1E for example) do the same: Try it online in Jelly or Try it online in 05AB1E.
– Kevin Cruijssen – 2020-01-23T13:23:08.100@Kaddath The magic square image depicts the diagonal going down and left. I just kept it the same for consistency. – Calvin's Hobbies – 2020-01-23T15:38:40.163
Must we start with
0
or may we choose to start from1
instead? – Shaggy – 2020-01-23T22:35:19.3731@Shaggy You do need to start at 0, sorry. – Calvin's Hobbies – 2020-01-23T23:42:05.990
3After 2 hours of attempts I've come to the sad conclusion that this is impossible in PHP... :( – Sam Dean – 2020-01-24T12:43:24.093
@SamDean I scratched my head for a while thinking about convoluted machiavelic plans that all failed too :D – Kaddath – 2020-01-24T14:30:39.827
@Kaddath The closest plan I had was some horrendous commenting option but because the only way to end "//" comments is with a new line I needed "/*/" comments but if you put that on the top line then that means one of your "down" programs has to start with an "" and I couldn't make that work – Sam Dean – 2020-01-24T14:53:06.360
@SamDean yeah I wanted a first line to be basically made of
"
for this reason, but couldn't find anything starting by "" because needed escaping in this line – Kaddath – 2020-01-24T15:08:06.240