33
\$\begingroup\$

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:

Wikipedia magic square example

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

\$\endgroup\$
19
\$\begingroup\$

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)
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @Calvin'sHobbies I've fixed my answer to fit the challenge specs \$\endgroup\$ – Lyxal Jan 22 at 22:20
  • 1
    \$\begingroup\$ Cool. What does the -hr do? \$\endgroup\$ – Calvin's Hobbies Jan 22 at 22:31
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Lyxal Jan 22 at 22:35
  • 1
    \$\begingroup\$ An explanation of the programs, like Jonathon Allan did, would probably help people understand it which feels important in a popularity contest! \$\endgroup\$ – Sam Dean Jan 24 at 10:54
  • 1
    \$\begingroup\$ @SamDean, I've added an explanation now. \$\endgroup\$ – Lyxal Jan 24 at 11:06
17
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – Kevin Cruijssen Jan 23 at 18:14
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Grimmy Jan 24 at 0:50
15
\$\begingroup\$

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 was 05AB1E code with .V, after which it discards 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:

  • 4) 8 4: push \$8\$; push \$4\$.
  • 7) 3Tα: push \$3\$; push \$10\$; take the absolute difference between the two: \$\lvert3-10\rvert\$.
  • 3) 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\$.
  • 2) XθнĀ·: push \$1\$; pop and push the first item: \$1\$; pop and push the last item: \$1\$; Python-style truthify it: \$1\$; double it: \$2×1\$.
  • 0) Tºß: push \$10\$; mirror it without overlap: \$1001\$; pop and push it's smallest digit: \$0\$.

Vertical:

  • 8) 8UX: push \$8\$; pop and store it in variable X; push variable X.
  • 10) 3θT: push \$3\$; pop and push its last digit: \$3\$; push \$10\$.
  • 9) T₃н: push \$10\$; push \$95\$; pop and push its first digit: \$9\$.
  • 11) α4Āº: take the absolute difference (no-op with an empty stack); push \$4\$; Python-style truthify it: \$1\$; mirror it without overlap: \$11\$.
  • 6) 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\$.
  • 5) 4α₃θ: push \$4\$; push the absolute difference (no-op with single item on the stack, but does pop); push \$95\$; pop and push its last digit: \$5\$.
| improve this answer | |
\$\endgroup\$
11
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JavaScript

I'm actually not sure it's possible. I suspect if a solution exists, it's purely arithmetic. So the content of any cell will be from this set 0123456789+-*/ ~!%^|&.

I tried for several hours without success.

The main problem is that for the last column (or row) to have any meaningful calculation, one of the rows (or columns) leading to it will end with an operator. And as far as I know there're just 2 postfix operators in JS, ++ & --, which work on variables, not primitive numbers.

The only other option is to have a semicolon (;) there.

Anyway, to make it easier for me to play around (entering characters and immediately get all the "eval" results), I created this fiddle: https://jsfiddle.net/targumon/0kj1pr28/

Maybe you'll succeed where I failed.

EDIT: It's currently hardcoded for entreing 4x4, but should be pretty straight forward to modify it to 5x5 or larger. Grid is created dynmaically now - just edit the let n = 5 at the top of the JS block to whatever size you want to play with.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ The challenge states Your submission must include at least one valid magic code square, so this is not a valid answer. The chat might be a better place to discuss incomplete submissions. On a different note, have you considered using comments to avoid the line ending in an operator problem? \$\endgroup\$ – Laikoni Feb 10 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.