# Draw diagonal positions of me squared

Based on the interpretation user @ThePirateBay made from the first version of my own challenge

### Output diagonal positions of me squared

now I challenge you to make ASCII art that draws diagonal lines based on a square character matrix based on a given number n.

A number n.

# Output

A square matrix of size n^2 which outputs diagonals represented by the adequate one of the \ X /chars. All other positions are to be fulfilled with # chars.

# Examples:

1
X

2
\/
/\

3
\#/
#X#
/#\

4
\##/
#\/#
#/\#
/##\

5
\###/
#\#/#
##X##
#/#\#
/###\


There will be no accepted answer. I want to know the shortest code for each language.

• Is outputting a list of lines ok? Aug 30 '17 at 22:01
• The background of octothorpes, the X in the middle if input is odd and each line in the big X is as long as input, not double the size. I think it's not a dupe. Aug 30 '17 at 22:51
• The output for even inputs makes this not a dupe (along with the other comments) Aug 30 '17 at 23:19
• I believe this to still be a duplicate. Here is a trivial change made to the MATL answer on the duplicate question. NB: I don't even know MATL. Aug 31 '17 at 0:16
• No, but it works for all that indexed into a string that looked something like "\/X ", e.g This Aug 31 '17 at 0:59

# Jelly, 14 bytes

⁼þµḤ+Ṛị“/\X#”Y


A monadic link taking a number and returning a list of characters; or a full program printing the result.

### How?

⁼þµḤ+Ṛị“/\X#”Y - Link: number, n
þ             - outer product (implicit range build on left AND right from n) with:
⁼              -   is equal (yields a table of 0s except the main diagonal is 1s)
µ            - monadic chain separation, call that I
Ḥ           - double (change all the 1s to 2s)
Ṛ         - reverse I (a table of 0s with 1s as the anti-diagonal)
+          - add (vectorises to make a table of zeros with 2s on the diagonal,
-      1s on the anti-diagonal and, if the meet, a 3 at the centre)
“/\X#”  - literal list of characters ['/','\','X','#']
ị        - index into (replaces... 1:'/'; 2:'\'; 3:'X'; and 0:'#')
Y - join with newline characters
- as a full program: implicit print

• Clever approach! Sep 25 '17 at 16:28

# Python 2, 83 bytes

n=input()
for i in range(n):l=['#']*n;l[i],l[~i]='\X/X'[i-~i==n::2];print''.join(l)


Try it online!

-3 thanks to Jonathan Frech and G B.

• Your TIO link has a footer artifact. Sep 25 '17 at 16:51
• @JonathanFrech I realized a version of mine was invalid and quickly rolled back... Sep 25 '17 at 16:59
• I noticed... :P Sep 25 '17 at 17:00
• Is not n/2.-.5 equivalent to ~-n/2.? Sep 25 '17 at 17:05
• @JonathanFrech missed that Sep 25 '17 at 17:06

# Ruby, 73 61 59 bytes

->n{(0...n).map{|x|w=?#*n;w[x],w[~x]=?\\,n==1+x*2??X:?/;w}}


Try it online!

# SOGL V0.12, 16 bytes

.↔╝Ζ #ŗ.2%?╬3←╬¡


Try it Here!

Finally got use out of the fact that palindromizating / or \ on the edge creates X (but this could be ~5 bytes less with a couple features I have had in mind for a while (e.g. that overlapping would overlap slashes too and palindomizating commands have the option to choose the overlap amount from the remainder of ceiling dividing))

Explanation:

.↔                push input ceiling-divided by 2 (. is required because SOGLs input is taken lazily)
╝               create a diagonal of that length
Ζ #ŗ           replace spaces with hashes
.2%?       if the input % 2 isn't 0
╬3       palindromize with 1 overlap
←      stop program
╬¡  [else] palindromize with 0 overlap


# R, 68 bytes

write(c("#","\\","/","X")[(d=diag(n<-scan()))+d[n:1,]*2+1],"",n,,"")


Try it online!

Reads n from stdin. Creates a matrix of indices (d=diag(n))+d[n:1,]*2+1 to index into the vector of characters to print, which results in a vector of characters. write writes it as a matrix to "" (stdout) with n columns and separator "".

# Charcoal, 21 bytes

ＵＢ#Ｎν↗∕ν²×X﹪ν²‖ＢＯL﹪ν²


Try it online! Verbose version.

• There's a neat trick to save two bytes by using multidirectional mirrors. In your case, either L or ¬ would work, but you would have to draw your line in a different direction of course.
– Neil
Oct 27 '17 at 11:57
• Note that the deverbosifier allows you to omit the else, so what you're seeing there is "if v is odd, draw an X and reflect with overlap (so that the X is not itself reflected) otherwise reflect without overlap".
– Neil
Oct 27 '17 at 11:59
• You can actually save a byte by using ReflectButterflyOverlap which takes a second parameter which is the amount of overlap. If you give it an overlap of 0 then you get a ReflectMirror and if you give it an overlap of 1 then you get a ReflectButterfly.
– Neil
Oct 27 '17 at 12:01
• Try it online!
– Neil
Nov 28 '17 at 11:12

# MATL, 14 bytes

'\/X#'iXytPE+)


Try it online!

Explanation: suppose the input is 3

'\/X#'               push this character string.
i                    read in input. stack is ['\/X#';3]
Xy                   push nxn identity matrix.
stack is ['\/X#'; [1 0 0 ; 0 1 0 ; 0 0 1]]
tP                   duplicate and flip left-right
stack is ['\/X#'; [1 0 0 ; 0 1 0 ; 0 0 1];
[0 0 1 ; 0 1 0 ; 1 0 0]]
E+                   double TOS and add top 2 stack elements
stack is ['\/X#'; [1 0 2 ; 0 3 0 ; 2 0 1]]
)                    index; 1-based modular, so
1 -> \, 2 -> /, 3 -> X, 0 -> #
implicit output.


# Javascript, 103 bytes

n=>{for(let i=n,j,s;i--;){s="";for(j=n;j--;)s+=i==j?n-j-1==j?"X":"\\":n-j-1==i?"/":"#";console.log(s)}}


Try it online!

n => {
for (let i = n, j, s; i--;) {
s="";
for (j = n; j--;)
s += i == j
? n - j - 1 == j
? "X"
: "\\"
: n - j - 1 == i
? "/"
: "#";
console.log(s)
}
}

• You can save a few bytes by using a return statement instead of logging it. Dec 21 '17 at 0:22
• Thanks @StephenLeppik, but I thought I had to output the result? Would I have to include "\n" after each line? Dec 21 '17 at 22:39
• You do have to output it, but on this site your return value is valid output. Dec 21 '17 at 22:51

# Husk, 20 bytes

´Ṫ§o!"¦/X#"+=ȯD=←¹+ŀ


Try it online!

# V, 24 bytes

Àé#ÀÄòr\jlò|òr/klòÇÜ/ãrX


Try it online!

# Mathematica, 114 bytes

(s=#;F[x_]:=DiagonalMatrix[Table[x,s]];z=F@"\\"+Reverse@F@"/"//. 0->"#";If[OddQ@s,z[[t=⌈s/2⌉,t]]="X"];Grid@z)&


# Python 2, 133 bytes

def f(k,h="#"):s=[h*i+"\\"+h*(k-i*2-2)+"/"+i*h for i in range(k/2)];print"\n".join(s+k%2*[k/2*h+"X"+k/2*h]+[e[::-1]for e in s[::-1]])


Try it online!

A beautiful mess done on mobile :-). If you have golfing suggestions, you are welcome to edit in yourselves (and credit you!), because I will be sleeping :P.

# Python 2, 122 bytes

def f(n):s='\\'+'#'*(n-2)+'/';return['x'][:n]if n<2else[s]+['#'+l+'#'for l in f(n-2)]+[s[::-1]]
print'\n'.join(f(input()))


Try it online!

A recursive aproach

# Perl 5, 81 bytes

for$i(0..($n=<>)-1){$_='#'x$n;s|.{$i}\K.|\\|;s%.(.{$i})$%/$1%;s%/%X% if!/\\/;say}


Try it online!

## Canvas, 15 bytes

：：#×＊；＼：↔１１╋１１╋


Try it online!

could get it to 11 bytes if canvas's overlap function had a default setting but it doesn't so...

• You don't need the starting ：： as the stack (practically) contains the inputs repeated forever. And yeah, I need to add a 1 byte built-in for １１╋ Apr 7 '18 at 12:22

# Mathematica, 99 bytes

FromCharacterCode[(2#+Reverse@#+2)&@DiagonalMatrix[45~Table~#]/.{2->35,137->88}]~StringRiffle~"\n"&


This uses a somewhat different technique from the other Mathematica solution. Also, we should probably start calling it the Wolfram Language at some point.