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.