# Make The Finest Magic Code Square

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 , 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.)

• @Lyxal The effective minimum is 2x2 since 1x1 is impossible. Jan 22 '20 at 21:51
• @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" Jan 22 '20 at 22:09
• @Calvin'sHobbies We missed you! Jan 22 '20 at 22:34
• @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. Jan 23 '20 at 13:23
• After 2 hours of attempts I've come to the sad conclusion that this is impossible in PHP... :( Jan 24 '20 at 12:43

# Keg, 4x4 magic square

0601
7.92
3._.
8654


Try it online!

## Outputs

 0601 - 1
7.92 - 7
3._. - 3
8654 - 4
/||||\
98652 0


I can't believe this actually works! Programs need the -hr flag to properly output the required result.

Test suite. Uncomment each line to run it. The -in flag is purely to make the test suite a nicer experience, as otherwise, the newlines would interfere with the programs.

## Programs

0601 - 1 (Push 0, 6, 0 and 1,then print the top of stack (TOS))
7.92 - 7 (Push 7 and print it. This makes the rest of the program a NOP)
3._. - 3 (Push 3 and print it. The _ will the try and pop from an empty stack, ending execution)
8654 - 4 (Push 8, 6, 5 and 4 and then print TOS)
0738 - 8 (Push 0, 7, 3 and 8 and then print TOS)
6..6 - 6 (Push 6 and print it. The . will try and print from an empty stack, end execution)
09_5 - 5 (Push 0 and 9, then pop the 9 and discard it then push 5. TOS is then printed)
12.4 - 2 (Push 1, then 2 and print TOS. The rest of the program is NOPs after that.)
0._4 - 0 (Like the program for 3, but with 0)
19.8 - 9 (Like the program for 2 but with 9)

• @Calvin'sHobbies I've fixed my answer to fit the challenge specs Jan 22 '20 at 22:20
• Cool. What does the -hr do? Jan 22 '20 at 22:31
• @Calvin'sHobbies Well, if, at the end of execution, nothing has been explicitly printed using either . or ,, -hr will output the last item on the stack raw. Jan 22 '20 at 22:35
• An explanation of the programs, like Jonathon Allan did, would probably help people understand it which feels important in a popularity contest! Jan 24 '20 at 10:54
• @SamDean, I've added an explanation now. Jan 24 '20 at 11:06

# Polyglot, 10x10

0     14
5
16
7
18
9
20
10
12
13


This yields the following row/column/diagonal programs (one per line):

0
01
0     2
3
4
5
6
7
8
9
10
011
12
13
0     14
15
16
17
18
19
20
021


In many stack-based languages, including 05AB1E and Jelly, each of those programs outputs the corresponding integer.

• Nice. :) Btw, your last line contains 1 space too many (11 characters). Noticed it when I was copy-pasting your answer in my 05AB1E test script. Without that final space it works fine, so +1 from me. Out of curiosity, did you find this by trial-and-error, a test/bruteforce script, or with a certain strategy in mind? Jan 23 '20 at 18:14
• @KevinCruijssen I quickly got the idea for this pattern, which gets all the rows/columns right but messes up the diagonals. I tried for a while to fix it manually, but eventually gave up. I then ran a brute-force search over all 4x4 patterns to find one that hits all-but-one of [0, 1, 2, 3, 10, 11, 12, 13, 20, 21]. The resulting pattern is at the bottom-left of my solution, the rest is just manual rearranging of my initial idea. Jan 24 '20 at 0:50

# 05AB1E, 4x4 5x5

8www4
w3Tαw
Uw₃4<
XθнĀ·
wTwºß


Try it online. (Loops over the rows; columns; main diagonal; and main anti-diagonal of the square, and will run it as 05AB1E code with .V, after which it will print the result and discard everything that remains on the stack for the next iteration with )\.)

### Explanation:

The w are no-ops, so I'll ignore them in the explanations below. In addition, the top of the stack is output implicitly for all of these programs as result.

Horizontal:

1. 8 4: push $$\8\$$; push $$\4\$$.
1. 3Tα: push $$\3\$$; push $$\10\$$; take the absolute difference between the two: $$\\lvert3-10\rvert\$$.
1. U₃4<: pop and store in variable X (stores an empty string in X with an empty stack); push $$\95\$$; push $$\4\$$; decrease the top by 1: $$\4-1\$$.
1. XθнĀ·: push $$\1\$$; pop and push its last digit: $$\1\$$; pop and push its first digit: $$\1\$$; Python-style truthify it: $$\1\$$; double it: $$\2×1\$$.
1. Tºß: push $$\10\$$; mirror it without overlap: $$\1001\$$; pop and push its smallest digit: $$\0\$$.

Vertical:

1. 8UX: push $$\8\$$; pop and store it in variable X; push variable X.
1. 3θT: push $$\3\$$; pop and push its last digit: $$\3\$$; push $$\10\$$.
1. T₃н: push $$\10\$$; push $$\95\$$; pop and push its first digit: $$\9\$$.
1. α4Āº: take the absolute difference (no-op with an empty stack); push $$\4\$$; Python-style truthify it: $$\1\$$; mirror it without overlap: $$\11\$$.
1. 4<·ß: push $$\4\$$; decrease it by 1: $$\4-1\$$; double it: $$\2×3\$$; pop and push its smallest digit: $$\6\$$.

Main diagonal / anti-diagonal:

1. 83₃Āß: push $$\83\$$; push $$\95\$$; Python-style truthify it: $$\1\$$; pop and push its smallest digit: $$\1\$$.
1. 4α₃θ: push $$\4\$$; push the absolute difference (will use the single argument on the stack twice): $$\\lvert4-4\rvert\$$; push $$\95\$$; pop and push its last digit: $$\5\$$.

# Jelly, A simple 3 by 3

Surely we can get a bigger one in Jelly, but if not...

5+2
+ḷ*
1+2


Try them online!
(splits on new lines (Ỵ) and evaluates (V) each (€) as Jelly programs)

5+2 : 5 + 2 = 7
5+1 : 5 + 1 = 6
5ḷ2 : left(5, 2) = 5
2*2 : 2 ^ 2 = 4
1+2 : 1 + 2 = 3
2ḷ1 : left(2, 1) = 2
+ḷ* : left((0 + 0), 0) ^ 0 = 1
+ḷ+ : left((0 + 0), 0) + 0 = 0