5
\$\begingroup\$

A magic square is an \$ n \times n \$ square grid, such that the sum of the integers on each row and column are equal. Note that the definition which will be used in this challenge is different than the one used by Wikipedia, since

  • diagonal sums are not accounted for
  • the numbers are not required to be distinct or in the range \$ 1, 2, ..., n^2 \$

Task

Write a program/function such that given an \$ n \times n \$ square, return Truthy if it is a magic square, and Falsey otherwise. There is a catch, however. The source code itself must also form a magic square, where each character in the source code corresponds to its Unicode codepoint.

Rules/Clarifications

  • You may take input in any reasonable format (e.g. 2D array, flattened list, etc.)
  • The source code should be a list of \$ n \$ strings, each consisting of \$ n \$ characters, joined by newlines.
  • The unicode character should be in its composed representation.
  • Here is a program to check if your program/function is a magic square

This is , so the shortest code in bytes wins!

\$\endgroup\$
15
  • 1
    \$\begingroup\$ Unicode characters can have multiple representations (composed, de-composed). If the source code has, say À, is that codepoint U+00C0 (LATIN CAPITAL LETTER A WITH GRAVE) or codepoints U+0041 U+0300 (LATIN CAPITAL LETTER A COMBINING GRAVE ACCENT)? \$\endgroup\$
    – Abigail
    Commented Jun 5, 2020 at 21:55
  • 1
    \$\begingroup\$ I suspect this has the same problem as this recent question had: programs are made of bytes not characters. The current answer you have could also equivalently be given by another program as you can see here (sorry it's a pastebin). Would the bytes of a program interpreted in any code page be a valid choice? I don't think this is clear as it is. \$\endgroup\$ Commented Jun 6, 2020 at 3:32
  • 2
    \$\begingroup\$ @FryAmTheEggman I agree. And it's not just bytes, it's a linear stream of bytes, while the question is about representation. For pure ASCII text, I don't see much problems, but in Unicode, it's difficult. Unicode has composed and decomposed characters, but not all decomposed combinations have a composed form. How does, say 😐́ (NEUTRAL FACE COMBINING ACUTE ACCENT) line up? It's one grapheme, 2 Unicode characters, and multiple bytes (how many will depend on the encoding (UTF-8, UTF-16, something else)). How many columns in your program does it take? \$\endgroup\$
    – Abigail
    Commented Jun 6, 2020 at 9:47
  • 1
    \$\begingroup\$ I assumed (from the checker code) that we must split our code at newline characters, but the post does not actually state that is what we are to do. (I imagine that for a subset of programs a code-page choice would still possible with that stipulation, but stating that would vastly reduce the possibility I believe.) \$\endgroup\$ Commented Jun 6, 2020 at 13:48
  • 1
    \$\begingroup\$ @pppery So, people are to reconstruct the rules based on program in a language they may not be familiar with? I can't deduce from the source code whether it's grouping columns by byte, character or grapheme. The fact the example input only uses ASCII where one grapheme is one character is one byte doesn't clarify anything. An example with zero-width characters, combining characters and wide characters would be useful. \$\endgroup\$
    – Abigail
    Commented Jun 7, 2020 at 0:20

3 Answers 3

9
\$\begingroup\$

05AB1E, 11 bytes

Executed code is the same as this answer. I was extremely lucky all the required characters were in the 05AB1E codepage.

ø«O
Ëq¶
/Öí

Try it online! Verify it!

\$\endgroup\$
2
  • \$\begingroup\$ The only answer here that has spent time making the magic square! +1 \$\endgroup\$
    – user92069
    Commented Jun 7, 2020 at 12:17
  • 2
    \$\begingroup\$ @Memberfor3months Not true, I spent quite some time trying things out for a smaller square than the one I posted, using calls to previous & next Links. \$\endgroup\$ Commented Jun 8, 2020 at 17:05
8
\$\begingroup\$

Jelly, 19 bytes

§E;Z
Z;E§
E§Z;
;Z§E

A monadic Link which yields 1 if the argument is a magic square as per the definition and 0 otherwise. Also works as a full program which prints the result (the first three helper links are actually unused).

The sums of the diagonals of the Unicode characters in the code are also equal to the row and column sums (385).

Try it online!

How?

;Z§E - Main Link: list of lists of numbers, M
 Z   - transpose M
;    - M concatenated with that
  §  - sums
   E - all equal?
\$\endgroup\$
3
\$\begingroup\$

05AB1E, 29 bytes

Just a port of the Jelly answer.

ø«OËq
qø«OË
Ëqø«O
OËqø«
«OËqø

Try it online!

Explanation

ø     Transpose the input
 «    Concatenate with the original input
  O   Sum (vectorizes over sublists)
   Ë  Are these sums all equal?
    q Exit the program (to create a garbage dump)

Now we got a HUGE garbage dump over here.

qø«OË
Ëqø«O
OËqø«
«OËqø
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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