3
\$\begingroup\$

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.

Input

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Is outputting a list of lines ok? \$\endgroup\$ – Mr. Xcoder Aug 30 '17 at 22:01
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – totallyhuman Aug 30 '17 at 22:51
  • 2
    \$\begingroup\$ The output for even inputs makes this not a dupe (along with the other comments) \$\endgroup\$ – Stephen Aug 30 '17 at 23:19
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – H.PWiz Aug 31 '17 at 0:16
  • 5
    \$\begingroup\$ No, but it works for all that indexed into a string that looked something like "\/X ", e.g This \$\endgroup\$ – H.PWiz Aug 31 '17 at 0:59

15 Answers 15

4
\$\begingroup\$

Jelly, 14 bytes

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

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

Try it online! or see [1-12] in one go.

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
\$\endgroup\$
  • \$\begingroup\$ Clever approach! \$\endgroup\$ – Erik the Outgolfer Sep 25 '17 at 16:28
4
\$\begingroup\$

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.

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

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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 21 bytes

UB#Nν↗∕ν²×X﹪ν²‖BOL﹪ν²

Try it online! Verbose version.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Neil Oct 27 '17 at 11:57
  • \$\begingroup\$ 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". \$\endgroup\$ – Neil Oct 27 '17 at 11:59
  • \$\begingroup\$ 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. \$\endgroup\$ – Neil Oct 27 '17 at 12:01
  • 1
    \$\begingroup\$ Try it online! \$\endgroup\$ – Neil Nov 28 '17 at 11:12
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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!

A more readable version with spaces and breaks added:

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)
  }
}
\$\endgroup\$
  • \$\begingroup\$ You can save a few bytes by using a return statement instead of logging it. \$\endgroup\$ – Nissa Dec 21 '17 at 0:22
  • \$\begingroup\$ Thanks @StephenLeppik, but I thought I had to output the result? Would I have to include "\n" after each line? \$\endgroup\$ – Wolff Dec 21 '17 at 22:39
  • \$\begingroup\$ You do have to output it, but on this site your return value is valid output. \$\endgroup\$ – Nissa Dec 21 '17 at 22:51
0
\$\begingroup\$

V, 24 bytes

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

Try it online!

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

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)&
\$\endgroup\$
0
\$\begingroup\$

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.

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

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

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

Perl 5, 81 bytes

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

Try it online!

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

Canvas, 15 bytes

::#×*;\:↔11╋11╋

Try it online!

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

\$\endgroup\$
  • \$\begingroup\$ 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 11╋ \$\endgroup\$ – dzaima Apr 7 '18 at 12:22
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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