16
\$\begingroup\$

A Latin square of order n is text which is arranged as a square of n lines by n columns, contains n different characters, and each character appears once in each line and column. For example, here is a Latin square of order 9:

Code gOlf
ode gOlfC
de gOlfCo
e gOlfCod
 gOlfCode
gOlfCode 
OlfCode g
lfCode gO
fCode gOl

Make a program which outputs a Latin square of order n (like in this other challenge), but the program must also be a Latin square of order n. The output must be different (i.e. not a quine) - use different symbols and/or in different order.

Some notes on formatting:

  1. Output to stdout or return a string which could be printed to stdout directly (i.e. it should contain newline bytes).
  2. Leading and trailing spaces are allowed only if the square doesn't otherwise contain spaces.
  3. Leading and trailing newlines are allowed.
  4. Tab and other control characters are allowed only if your system can print them without disturbing the square shape.
  5. Usual Code Golf - your score is the number of bytes in your program, so a score should normally be n²+n-1 or n²+n.
  6. Include your program's output in your answer, if you think it looks pretty.

As noted by tsh, n = 1 is uninteresting, so let's add a restriction n > 1.

\$\endgroup\$
6
  • \$\begingroup\$ Won’t this question just encourage 1 byte answers like Retina or so? Had I missed anything in the question which prevent 1 byte answers? \$\endgroup\$
    – tsh
    Commented Feb 14, 2023 at 7:34
  • \$\begingroup\$ I missed that possibility; I guess it's bad style to disallow that now, but I'll just go ahead and do that. \$\endgroup\$
    – anatolyg
    Commented Feb 14, 2023 at 7:40
  • \$\begingroup\$ @anatolyg There are no answers yet, so I think you can still ban it. \$\endgroup\$
    – Adám
    Commented Feb 14, 2023 at 7:41
  • 1
    \$\begingroup\$ Does a character need to appear exactly once in every line and column or the same number of times in each line or column? \$\endgroup\$
    – mousetail
    Commented Feb 14, 2023 at 8:11
  • 2
    \$\begingroup\$ Once in every column, once in every row. Also described in Wikipedia; see there if there is any doubt. \$\endgroup\$
    – anatolyg
    Commented Feb 14, 2023 at 22:02

7 Answers 7

15
\$\begingroup\$

C89 (using implicit int), 811 bytes (\$ n = 28 \$)

ps0,f;to(char/*1+){2-u9]y=}[
+,}2=s09f-(put1;*/oy[]){char
u[9012]={f};/*c-as)rt(,op+hy
th,+p-;])0r}a92{s1([fc*/oy=u
;for(ps0/*t]9y=u,h1-}2[c{a)+
o1ayc{-}[]f+th,r)(u*/=29;ps0
-=1;u[ps0/*2)fa+hrc,9o}(]ty{
}c2)y9rtp;,[so+h-0fu{a(1*/]=
92-ps0+/*h1t;)o}{=yc(ru,a][f
s]+9a)1(}r-,[upy;f0=c{oht*/2
);for(/*1p9{]0[s2-}+a,=yhuct
]{(h[,ay+sp-}r)0ucto1*/f=29;
f-=1;oy[29/*ps(,t{+0)}]arcuh
au[=of,h;+{sc}y9(t2)p0r*/-1]
=oy[0])puts/*c;(92r1+-h}f{,a
29cf]a[{,u0rh-stp*/(oy+=1);}
/*r{th};-)=u,]920+pfy1a[so(c
,+s/h*{2]a;1=[0py9-trfc)u}o(
{t]u+c2)s1yo-,/[r}a9=hp0(;f*
1(*c{/u+a2])o=}f[,9shty;-0rp
y0t-/r(f={a9+1uoc][h*p;s},2)
*)ha9}fcy,u({+r=o[]p;/0-21ts
(/{]2yc-o}+hfpta=u*;s91r)[0,
cy)t*=91r[2f0a-]};h/,s{u+(po
[au*}+=rh(cy2{f)/o,]0;tp9s-1
0}/s,1hatyoc(;]*fp={u)-2[r+9
rp;()u*,co[=y2h/1as}]+ft09{-
hrp}-tou9=)01({c]y;a2[s+,f*/

Try it online

This is a function called to which prints the following output:

]\[ZYXWVUTSRQPONMLKJIHGFEDCB
\[ZYXWVUTSRQPONMLKJIHGFEDCB]
[ZYXWVUTSRQPONMLKJIHGFEDCB]\
ZYXWVUTSRQPONMLKJIHGFEDCB]\[
YXWVUTSRQPONMLKJIHGFEDCB]\[Z
XWVUTSRQPONMLKJIHGFEDCB]\[ZY
WVUTSRQPONMLKJIHGFEDCB]\[ZYX
VUTSRQPONMLKJIHGFEDCB]\[ZYXW
UTSRQPONMLKJIHGFEDCB]\[ZYXWV
TSRQPONMLKJIHGFEDCB]\[ZYXWVU
SRQPONMLKJIHGFEDCB]\[ZYXWVUT
RQPONMLKJIHGFEDCB]\[ZYXWVUTS
QPONMLKJIHGFEDCB]\[ZYXWVUTSR
PONMLKJIHGFEDCB]\[ZYXWVUTSRQ
ONMLKJIHGFEDCB]\[ZYXWVUTSRQP
NMLKJIHGFEDCB]\[ZYXWVUTSRQPO
MLKJIHGFEDCB]\[ZYXWVUTSRQPON
LKJIHGFEDCB]\[ZYXWVUTSRQPONM
KJIHGFEDCB]\[ZYXWVUTSRQPONML
JIHGFEDCB]\[ZYXWVUTSRQPONMLK
IHGFEDCB]\[ZYXWVUTSRQPONMLKJ
HGFEDCB]\[ZYXWVUTSRQPONMLKJI
GFEDCB]\[ZYXWVUTSRQPONMLKJIH
FEDCB]\[ZYXWVUTSRQPONMLKJIHG
EDCB]\[ZYXWVUTSRQPONMLKJIHGF
DCB]\[ZYXWVUTSRQPONMLKJIHGFE
CB]\[ZYXWVUTSRQPONMLKJIHGFED
B]\[ZYXWVUTSRQPONMLKJIHGFEDC

I started with code like this:

i;
to()
{
    char u[100] = {0};
    char *s = u;
    // Fill the string with some printable bytes
    for (i = 29; i -= 1; u[i] = 94 - i)
        ;
    for (i = 29; i -= 1; )
    {
        puts(s += 1); // print the string
        s[29 - 1] = s[0]; // rotate the string
    }
}

I arranged it as a sequence of strings which all have unique characters, padded with spaces in such a way that I could replace spaces with comments later. Then I tweaked the code repeatedly, until all columns also contained unique characters. This was not easy!

The result was this:

ps0,f;to(char
                  oy[]){char
u[9012]={f};
                        oy=u
;for(ps0
                     =29;ps0
-=1;u[ps0
                          ]=
92-ps0+
                           2
);for(
                       f=29;
f-=1;oy[29
                         -1]
=oy[0])puts
                   (oy+=1);}

I had to use multi-character variable names to make all the string fragments have different lengths. I tried to use a minimal subset of characters, but ultimately I failed to satisfy all constraints and added an extra character y. So it seems possible to golf this further (if you want to do this — good luck, I'm out!)

Then, I made a script which completes any partially filled rectangle to a Latin square, randomizing characters inside the comments and doing backtracking.

\$\endgroup\$
0
12
\$\begingroup\$

APL (Dyalog Unicode), 109 bytes

Full program that prints to stdout.

(↑⍋⌽¨⊂) ⎕D
↑⍋⌽¨⊂) ⎕D(
⍋⌽¨⊂) ⎕D(↑
⌽¨⊂) ⎕D(↑⍋
¨⊂) ⎕D(↑⍋⌽
⊂) ⎕D(↑⍋⌽¨
) ⎕D(↑⍋⌽¨⊂
 ⎕D(↑⍋⌽¨⊂)
⎕D(↑⍋⌽¨⊂) 
D(↑⍋⌽¨⊂) ⎕

Try it online!

⎕D the digits 0 through 9

() apply the following tacit function:

 on the entire argument:

  ⌽¨ rotate each of the following amounts:

    the grade (the permutation that would sort the argument, i.e. the indices 1 through 10)

 mix the list of strings into a character matrix

The first rotation ends up having unbalanced parentheses and therefore quits with an error, causing no further output on stdout.

\$\endgroup\$
10
\$\begingroup\$

Thunno, 11 bytes (\$ n = 3 \$)

drz
rzd
zdr

Attempt This Online!

Reads its own source code and reverses it. (I can't see anything in the question which disallows this.)

Output

rdz
dzr
zrd

Explanation

Only the last line matters:

zd   # Read source code and push to stack
  r  # Reverse so the output is different
     # Implicit output
\$\endgroup\$
8
\$\begingroup\$

J, 239 bytes (n=15)

choue=:i.+|~/15
i=:15.|+/~choue
=echo|.u:i+/~51
51|i.+/~=:eouhc
15/.|~+:eui=coh
+.5|:1=cu/~iheo
|ih~/o51+c.:e=u
o|ie15~/c.hu+:=
~+15cuoh|e/.=i:
huec+/1o~5=|:.i
u/~=hci|o1:e5+.
/~.:=eu51hoci|+
:o+/~ihe5=u1.c|
.cu+i:e=ho15|/~
e:=ouhc.i|5+1~/

Try it online!


Explanation

choue=:i.+|~/15
  • |~/ is a no-op on 15: it inserts |~ between the items, but there's only one item.
  • + is complex conjugation, which is also a no-op on 15.
  • i. makes a list of the first 15 nonnegative integers.
  • That list is assigned to choue.
i=:15.|+/~choue
  • +/~ makes an addition table of the integers in choue.
  • 15. | reduces it modulo 15; the full stop is used as a decimal point.
  • The result is assigned to i.
=echo|.u:i+/~51
  • +/~ here ends up being the same as plain addition, adding 51 to each item of i.
  • u: converts numbers to characters.
  • |. reverses the result. (Something had to be put here to separate echo from u:.)
  • echo outputs the table of characters.
  • = doesn't matter because it is applied after the echo.
51|i.+/~=:eouhc
  • This line is a syntax error, ending execution.
\$\endgroup\$
5
\$\begingroup\$

Jelly, 29 bytes

Rṙ`Y5
ṙ`Y5R
`Y5Rṙ
Y5Rṙ`
5Rṙ`Y

Try it online!

All lines but the last are ignored.

5R       [1 .. 5]
  ṙ      rotated left
   `     each of [1 .. 5] times.
    Y    Join on newlines.
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 55 bytes (\$ n = 7 \$)

0oā._»2
oā._»20
ā._»20o
._»20oā
_»20oā.
»20oā._
20oā._»

Try it online!

Output

0485761
4857610
8576104
5761048
7610485
6104857
1048576

Explanation

We can ignore everything but the last line.

20       # Push 20
  o      # Push 2 ** 20 (1048576)
   ā     # Push [1..7]
    ._   # Rotate 1048576 that many times
      »  # Join by newlines
         # Implicit output
\$\endgroup\$
6
  • \$\begingroup\$ It might be worth explaining why 6LD._» inserts extra spaces for people who don't know the language. Very good answer, but it left me very confused! \$\endgroup\$ Commented Feb 14, 2023 at 21:58
  • 1
    \$\begingroup\$ @UnrelatedString I think that's because 6L is a list, and so you will need to join each inner list after rotating. 1048576 is interpreted as a string and so it doesn't need joining (see here) \$\endgroup\$
    – The Thonnu
    Commented Feb 15, 2023 at 7:30
  • 1
    \$\begingroup\$ 7LD._J» will work though \$\endgroup\$
    – The Thonnu
    Commented Feb 15, 2023 at 7:32
  • \$\begingroup\$ My answer will remain deleted.. I've been unable to find a way to push 6 unique characters with 2 loose single-byte builtins, and with a single 2-byte builtin like žO you will get errors on the loose trailing ž. And although I did find a way to push a 5x5 Latin square with 5 characters (8zv=Á), unfortunately it will print crap when expanding the source code to a Latin square.. (An alternative for 20o could be 8z(, though: try it online.) \$\endgroup\$ Commented Feb 15, 2023 at 12:01
  • 1
    \$\begingroup\$ @TheThonnu Ah, you're right. Either way, there isn't a way to get a 6x6 square from what I could find. \$\endgroup\$ Commented Feb 15, 2023 at 12:20
3
\$\begingroup\$

Vyxal j, 41 bytes (\$ n = 6 \$)

∪:żvǓk
:żvǓk∪
żvǓk∪:
vǓk∪:ż
Ǔk∪:żv
k∪:żvǓ

Try it Online!

Output

eiouya
iouyae
ouyaei
uyaeio
yaeiou
aeiouy

Explanation

Only the last line matters, so everything else can be ignored.

k∪      # Push "aeiouy"
  :ż    # Duplicate and push [1..6]
    vǓ  # Rotate the string that many places
        # Implicit output, joined by newlines
\$\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.