Make The Finest Magic Code Square

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:

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

Calvin's Hobbies

Posted 2020-01-22T21:34:27.353

Reputation: 84 000

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

2@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 from 1 instead? – Shaggy – 2020-01-23T22:35:19.373

1@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

Answers

14

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.

Grimmy

Posted 2020-01-22T21:34:27.353

Reputation: 12 521

1

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?

– Kevin Cruijssen – 2020-01-23T18:14:00.717

2

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

– Grimmy – 2020-01-24T00:50:50.980

13

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)

Lyxal

Posted 2020-01-22T21:34:27.353

Reputation: 5 253

@Calvin'sHobbies I've fixed my answer to fit the challenge specs – Lyxal – 2020-01-22T22:20:34.417

1Cool. What does the -hr do? – Calvin's Hobbies – 2020-01-22T22:31:32.820

2@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. – Lyxal – 2020-01-22T22:35:40.457

1

An explanation of the programs, like Jonathon Allan did, would probably help people understand it which feels important in a popularity contest!

– Sam Dean – 2020-01-24T10:54:23.197

1@SamDean, I've added an explanation now. – Lyxal – 2020-01-24T11:06:14.380

13

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\$.

Kevin Cruijssen

Posted 2020-01-22T21:34:27.353

Reputation: 67 575

10

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

Jonathan Allan

Posted 2020-01-22T21:34:27.353

Reputation: 67 804

1

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.

targumon

Posted 2020-01-22T21:34:27.353

Reputation: 155

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?

– Laikoni – 2020-02-10T08:37:29.073