Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

CJam, 32 30 29 28 bytes

ri_"/\ /"2/f*)@,\f>+_z..e>N*

[Test it here.][1]

I was trying to help Reto golf his CJam answer but ended up with a solution that had nothing to do with his, so I figured I might as well post it myself.

Explanation

This makes use of the symmetry of the output. In particular, the fact that the output is the same as its transpose.

First, we generate the first N+1 lines, but without the left edge:

ri       e# Read input and convert to integer N.
_        e# Duplicate.
"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
f*       e# Repeat each of the two strings N times. That gives the first two rows.
)        e# Detach the second row.
@,       e# Pull up the other copy of N and turn into range [0 1 ... N-1].
\f>      e# For each element i in that range, discard the first i characters of
         e# the second row.
+        e# Add all those lines back to the first row.

Now we've got an array of strings representing the following grid:

/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /

The transpose of that looks like this:

/ / /
\/ / 
/ / /
\/ / 
/ / /
\/ /
/ /
\/

Together, these have all the non-space characters that we need. We can now make use of Dennis's rad tipDennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. In all positions where the two grids differ, one will have a space (or nothing at all) and the other will have the character we're looking for. When one list in a vectorised operation is longer than the other, the additional elements of the longer list will simply be kept, which is just what we're looking for. In the other cases, the non-space character will always be the maximum of the two characters:

_z   e# Duplicate the grid and transpose it.
..e> e# For each pair of characters in corresponding positions, pick the maximum.
N*   e# Join the lines by linefeed characters.

[1]: http://cjam.aditsu.net/#code=ri_%22%2F%5C%20%2F%222%2Ff*)%40%2C%5Cf%3E%2B_z..e%3EN*&input=4

CJam, 32 30 29 28 bytes

ri_"/\ /"2/f*)@,\f>+_z..e>N*

[Test it here.][1]

I was trying to help Reto golf his CJam answer but ended up with a solution that had nothing to do with his, so I figured I might as well post it myself.

Explanation

This makes use of the symmetry of the output. In particular, the fact that the output is the same as its transpose.

First, we generate the first N+1 lines, but without the left edge:

ri       e# Read input and convert to integer N.
_        e# Duplicate.
"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
f*       e# Repeat each of the two strings N times. That gives the first two rows.
)        e# Detach the second row.
@,       e# Pull up the other copy of N and turn into range [0 1 ... N-1].
\f>      e# For each element i in that range, discard the first i characters of
         e# the second row.
+        e# Add all those lines back to the first row.

Now we've got an array of strings representing the following grid:

/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /

The transpose of that looks like this:

/ / /
\/ / 
/ / /
\/ / 
/ / /
\/ /
/ /
\/

Together, these have all the non-space characters that we need. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. In all positions where the two grids differ, one will have a space (or nothing at all) and the other will have the character we're looking for. When one list in a vectorised operation is longer than the other, the additional elements of the longer list will simply be kept, which is just what we're looking for. In the other cases, the non-space character will always be the maximum of the two characters:

_z   e# Duplicate the grid and transpose it.
..e> e# For each pair of characters in corresponding positions, pick the maximum.
N*   e# Join the lines by linefeed characters.

[1]: http://cjam.aditsu.net/#code=ri_%22%2F%5C%20%2F%222%2Ff*)%40%2C%5Cf%3E%2B_z..e%3EN*&input=4

CJam, 32 30 29 28 bytes

ri_"/\ /"2/f*)@,\f>+_z..e>N*

[Test it here.][1]

I was trying to help Reto golf his CJam answer but ended up with a solution that had nothing to do with his, so I figured I might as well post it myself.

Explanation

This makes use of the symmetry of the output. In particular, the fact that the output is the same as its transpose.

First, we generate the first N+1 lines, but without the left edge:

ri       e# Read input and convert to integer N.
_        e# Duplicate.
"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
f*       e# Repeat each of the two strings N times. That gives the first two rows.
)        e# Detach the second row.
@,       e# Pull up the other copy of N and turn into range [0 1 ... N-1].
\f>      e# For each element i in that range, discard the first i characters of
         e# the second row.
+        e# Add all those lines back to the first row.

Now we've got an array of strings representing the following grid:

/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /

The transpose of that looks like this:

/ / /
\/ / 
/ / /
\/ / 
/ / /
\/ /
/ /
\/

Together, these have all the non-space characters that we need. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. In all positions where the two grids differ, one will have a space (or nothing at all) and the other will have the character we're looking for. When one list in a vectorised operation is longer than the other, the additional elements of the longer list will simply be kept, which is just what we're looking for. In the other cases, the non-space character will always be the maximum of the two characters:

_z   e# Duplicate the grid and transpose it.
..e> e# For each pair of characters in corresponding positions, pick the maximum.
N*   e# Join the lines by linefeed characters.

[1]: http://cjam.aditsu.net/#code=ri_%22%2F%5C%20%2F%222%2Ff*)%40%2C%5Cf%3E%2B_z..e%3EN*&input=4

deleted 57 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

Test it here. [Test it here.][1]

First, we generate everything exceptthe first N+1 lines, but without the left edge:

ri       e# Read input and convert to integer N.
_        e# Duplicate.
"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
ri       e# Read input and convert to integer N.
f*       e# Repeat each of the two strings N times. That gives the first two rows.
~)        e# UnwrapDetach the array, dumping both arrays on thesecond stackrow.
{ @,       e# While the topPull ofup the stack is truthy (non-empty)...
  _1>   other e#copy of N Copyand theturn lineinto andrange discard[0 its1 first... characterN-1].
}h
&  \f>      e# The last two lines are "/" and "".For Theeach secondelement onei isn'tin anthat issuerange, butdiscard the first
   i characters of
    e# one is so we takee# the set intersection to combine them into a singlesecond ""row.
]+        e# WrapAdd all those lines inback anto arraythe againfirst row.
/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /

The transpose of that looks like this:

/ / /
\/ / 
/ / /
\/ /  
/ / /
\/ /
/ /
\/

The transpose of that has the necessary backslashes on the left edgeTogether, but lacks them atthese have all the topnon-space characters that we need. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. The original and the transpose only differ in thoseIn all positions where we need a backslashthe two grids differ, one will have a space (or nothing at all) and the other grid will have the character we're looking for. When one list in a space therevectorised operation is longer than the other, the additional elements of the longer list will simply be kept, which is just what we're looking for. HenceIn the backslashother cases, the non-space character will always be the maximum of the two characters:

Due to the way[1]: . behaves for lists of different lengths, it would actually be enough to build up only the first half of the lines before this last step, but I haven't yet found a way to do so in fewer byteshttp://cjam.aditsu.net/#code=ri_%22%2F%5C%20%2F%222%2Ff*)%40%2C%5Cf%3E%2B_z..e%3EN*&input=4

Test it here.

First, we generate everything except the left edge:

"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
ri       e# Read input and convert to integer N.
f*       e# Repeat each of the two strings N times. That gives the first two rows.
~        e# Unwrap the array, dumping both arrays on the stack.
{        e# While the top of the stack is truthy (non-empty)...
  _1>    e#   Copy the line and discard its first character.
}h
&        e# The last two lines are "/" and "". The second one isn't an issue but the first
         e# one is so we take the set intersection to combine them into a single "".
]        e# Wrap all lines in an array again.
/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /
 / /
/ /
 /

The transpose of that has the necessary backslashes on the left edge, but lacks them at the top. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. The original and the transpose only differ in those positions where we need a backslash, and the other grid will have a space there. Hence the backslash will always be the maximum of the two characters:

Due to the way . behaves for lists of different lengths, it would actually be enough to build up only the first half of the lines before this last step, but I haven't yet found a way to do so in fewer bytes.

[Test it here.][1]

First, we generate the first N+1 lines, but without the left edge:

ri       e# Read input and convert to integer N.
_        e# Duplicate.
"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
f*       e# Repeat each of the two strings N times. That gives the first two rows.
)        e# Detach the second row.
@,       e# Pull up the other copy of N and turn into range [0 1 ... N-1].
\f>      e# For each element i in that range, discard the first i characters of
         e# the second row.
+        e# Add all those lines back to the first row.
/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /

The transpose of that looks like this:

/ / /
\/ / 
/ / /
\/ /  
/ / /
\/ /
/ /
\/

Together, these have all the non-space characters that we need. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. In all positions where the two grids differ, one will have a space (or nothing at all) and the other will have the character we're looking for. When one list in a vectorised operation is longer than the other, the additional elements of the longer list will simply be kept, which is just what we're looking for. In the other cases, the non-space character will always be the maximum of the two characters:

[1]: http://cjam.aditsu.net/#code=ri_%22%2F%5C%20%2F%222%2Ff*)%40%2C%5Cf%3E%2B_z..e%3EN*&input=4

added 8 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975

CJam, 32 30 2929 28 bytes

"ri_"/\ /"2/rif*~{_1>}h&]_zf*)@,\f>+_z..e>N*

Test it here.

I was trying to help Reto golf his CJam answer but ended up with a solution that had nothing to do with his, so I figured I might as well post it myself.

Explanation

This makes use of the symmetry of the output. In particular, the fact that the output is the same as its transpose.

First, we generate everything except the left edge:

"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
ri       e# Read input and convert to integer N.
f*       e# Repeat each of the two strings N times. That gives the first two rows.
~        e# Unwrap the array, dumping both arrays on the stack.
{        e# While the top of the stack is truthy (non-empty)...
  _1>    e#   Copy the line and discard its first character.
}h
&        e# The last two lines are "/" and "". The second one isn't an issue but the first
         e# one is so we take the set intersection to combine them into a single "".
]        e# Wrap all lines in an array again.

Now we've got an array of strings representing the following grid:

/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /
 / /
/ /
 /

The transpose of that has the necessary backslashes on the left edge, but lacks them at the top. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. The original and the transpose only differ in those positions where we need a backslash, and the other grid will have a space there. Hence the backslash will always be the maximum of the two characters:

_z   e# Duplicate the grid and transpose it.
..e> e# For each pair of characters in corresponding positions, pick the maximum.
N*   e# Join the lines by linefeed characters.

Due to the way . behaves for lists of different lengths, it would actually be enough to build up only the first half of the lines before this last step, but I haven't yet found a way to do so in fewer bytes.

CJam, 32 30 29 bytes

"/\ /"2/rif*~{_1>}h&]_z..e>N*

Test it here.

I was trying to help Reto golf his CJam answer but ended up with a solution that had nothing to do with his, so I figured I might as well post it myself.

Explanation

This makes use of the symmetry of the output. In particular, the fact that the output is the same as its transpose.

First, we generate everything except the left edge:

"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
ri       e# Read input and convert to integer N.
f*       e# Repeat each of the two strings N times. That gives the first two rows.
~        e# Unwrap the array, dumping both arrays on the stack.
{        e# While the top of the stack is truthy (non-empty)...
  _1>    e#   Copy the line and discard its first character.
}h
&        e# The last two lines are "/" and "". The second one isn't an issue but the first
         e# one is so we take the set intersection to combine them into a single "".
]        e# Wrap all lines in an array again.

Now we've got an array of strings representing the following grid:

/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /
 / /
/ /
 /

The transpose of that has the necessary backslashes on the left edge, but lacks them at the top. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. The original and the transpose only differ in those positions where we need a backslash, and the other grid will have a space there. Hence the backslash will always be the maximum of the two characters:

_z   e# Duplicate the grid and transpose it.
..e> e# For each pair of characters in corresponding positions, pick the maximum.
N*   e# Join the lines by linefeed characters.

Due to the way . behaves for lists of different lengths, it would actually be enough to build up only the first half of the lines before this last step, but I haven't yet found a way to do so in fewer bytes.

CJam, 32 30 29 28 bytes

ri_"/\ /"2/f*)@,\f>+_z..e>N*

Test it here.

I was trying to help Reto golf his CJam answer but ended up with a solution that had nothing to do with his, so I figured I might as well post it myself.

Explanation

This makes use of the symmetry of the output. In particular, the fact that the output is the same as its transpose.

First, we generate everything except the left edge:

"/\ /"2/ e# Push an array with two strings: ["/\" " /"]
ri       e# Read input and convert to integer N.
f*       e# Repeat each of the two strings N times. That gives the first two rows.
~        e# Unwrap the array, dumping both arrays on the stack.
{        e# While the top of the stack is truthy (non-empty)...
  _1>    e#   Copy the line and discard its first character.
}h
&        e# The last two lines are "/" and "". The second one isn't an issue but the first
         e# one is so we take the set intersection to combine them into a single "".
]        e# Wrap all lines in an array again.

Now we've got an array of strings representing the following grid:

/\/\/\/\
 / / / /
/ / / /
 / / /
/ / /
 / /
/ /
 /

The transpose of that has the necessary backslashes on the left edge, but lacks them at the top. We can now make use of Dennis's rad tip to combine two ASCII grids into one, by taking the maximum of each corresponding pair of characters. The original and the transpose only differ in those positions where we need a backslash, and the other grid will have a space there. Hence the backslash will always be the maximum of the two characters:

_z   e# Duplicate the grid and transpose it.
..e> e# For each pair of characters in corresponding positions, pick the maximum.
N*   e# Join the lines by linefeed characters.

Due to the way . behaves for lists of different lengths, it would actually be enough to build up only the first half of the lines before this last step, but I haven't yet found a way to do so in fewer bytes.

added 151 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975
Loading
added 1747 characters in body
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975
Loading
Source Link
Martin Ender
  • 197.2k
  • 67
  • 447
  • 975
Loading