23
\$\begingroup\$

Task is pretty simple. Your program should take a number N from 1 to 20 and draw the following ASCII art.
When N=1:

_____
| | |
| | |
| | |
| | |
| | |
| | |
| | |
|_|_|
\   /
 \-/
  v

When N=2:

_______
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|  |  |
|__|__|
\     /
 \---/
  \ /
   v

When N=3:

_________
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|   |   |
|___|___|
\       /
 \-----/
  \   /
   \ /
    v

And etc. Trailing spaces are allowed.

This is a code-golf challenge so the shortest solution wins.

\$\endgroup\$
3
  • \$\begingroup\$ Related \$\endgroup\$
    – sinvec
    Feb 20 at 14:25
  • 27
    \$\begingroup\$ There are 8n pipes, for anyone too lazy to count. \$\endgroup\$ Feb 21 at 9:20
  • \$\begingroup\$ Well, there are 8n rows of pipes, 24n pipes in total. \$\endgroup\$ May 27 at 9:02

16 Answers 16

6
\$\begingroup\$

Charcoal, 30 bytes

Nθ×_θ↑×⁸θ←×_⊕θ↓_↓×⁸θ↘¹P⊕θ↘θv‖B

Try it online! Link is to verbose version of code. Explanation:

Nθ

Input n.

×_θ

Print the n _s near the bottom left of the pencil.

↑×⁸θ

Print the 3n |s up the middle of the pencil.

←×_⊕θ↓_

Print the left half of the _s at the top of the pencil.

↓×⁸θ

Print the left 3n |s of the pencil.

↘¹

Print the first \ at the bottom left of the pencil.

P⊕θ

Print the left half of the -s near the bottom of the pencil without moving the cursor.

↘θ

Print the rest of the \s at the bottom left of the pencil.

v

Print the v at the bottom of the pencil.

‖B

Reflect to complete the pencil.

\$\endgroup\$
5
\$\begingroup\$

Zsh, 175 bytes

x=\|${(l:n=$1:)}
x+=$x\|
<<<${x//?/_}
repeat n\*8-1 <<<$x
<<<${x// /_}
q=\\${(l:n*2+1:)}/
<<<$q
q=${q// /-}
repeat n q=${${q/?\///}/\\?/ \\}&&<<<$q&&q=${q//-/ }
<<<${q/\\*/ v}

Attempt This Online!

30 bytes spent on the line on the tip :(
Attempt This Online!

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 44 bytes

Ì'_×'|IúRĆI8*.Dð'_:'\I>úRηRεNið'-:]`\'v).º.c

Try it online or verify all test cases.

Explanation:

Ì         # Increase the (implicit) input by 2
 '_×     '# Push a string with that many amount of "_"
'|       '# Push "|"
  Iú      # Pad it with the input amount of leading spaces
    R     # Reverse the string, so the spaces are trailing
     Ć    # Enclose; append its own head "|"
I8*       # Push the input, and multiply it by 8
   .D     # Duplicate the string that many times on the stack
ð'_:     '# On the top copy, replace all spaces with "_"
'\       '# Push "\"
  I>ú     # Pad it with the input+1 amount of leading spaces
     R    # Reverse the string, so the spaces are trailing
      η   # Pop and push a list of its prefixes
       R  # Reverse this list of prefixes
 ε        # Map over each string:
  Ni      #  If the 0-based map-index is 1 (so the second string):
    ð'-: '#   Replace all spaces with "-"
 ]        # Close both the if-statement and map
  `       # Pop and push all values separated to the stack
   \      # Discard the top copy (the loose "\")
    'v   '# Push a "v" instead
)         # Wrap all strings on the stack into a list
 .º       # Mirror each line with overlapping center
   .c     # Pad leading spaces to centralize everything, and join by newlines
          # (after which the result is output implicitly)
\$\endgroup\$
5
\$\begingroup\$

brainfuck, 573 bytes

[>+<-]>[>+>+<<-]>>[<+<+>>-]>>>++++++++++[>++++++++++>+<<-]>-----...<<<<<[>>>>>.<<<<<-]>>>>>>.<<<++++++[->+++++>+++++<<]>++>-<<<<<<[>++++++++>+<<-]>>[<<+>>-]<-[-<[->>+>+>+<<<<]>>[<<+>>-]>>>>.<<[>.<-]>>.<<<[>>.<<-]>>>.>.<<<<<<]>>>>[<<+++<+>>>-]>.<<<-<<<[>+>>>+>+<<<<<-]>>>>[<.>-]>>.<[<<.>>-]>.>.<[>>+<<-](>>------<<)<++++[<++++>-]<-<---.<[>>+>>+<<<<-]<[<+<+>>-]<[>++>++<<-]>->+[>>>>.<<<<-]>>.>>>.<.<<--<.<<[>>>.<<<-]>>>++.>>>.<<<<<<<<[>+>+<<-]>[<+>-]>-[<<[>+>>+<<<-]>[<+>-]>[<+>>->>>++<<<<-]<[>+<-]>>+[>>>>.<<<<-]>.>>-[>.<-]<.>>>.<<<<<[<+>-]<-]<<+[>>>>>>>.<<<<<<<-]>>>>>>>>>.

Try it online!

Hi, this is my first doing code-golf challenge. yeah, I think it can be shorter if It has a better variable position arrangement and better ways of creating "_", "|", " ", "/", "", "-", "v", "\n", but I think I will try to figure it out later.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to the site, and nice first answer! \$\endgroup\$
    – sinvec
    Feb 27 at 15:10
4
\$\begingroup\$

Ruby, 127 110 bytes

->n{puts ?_*(n*2+3),(-8*n..n).map{|i|a=i*i<2?" -_"[i]:S=" ";i<0?[?|]*3*a*=n:S*i+?\\+a*(n-i<<1|1)+?/},S*-~n+?v}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 116 bytes \$\endgroup\$
    – Dingus
    Feb 21 at 13:01
  • \$\begingroup\$ @Dingus thanks, I bashed this out at 1am and knew it could be improved. Use of a constant S was inspired, because a variable isn't visible outside the loop it's declared in. I've made a further improvement (rationalized a and b into a single variable.) I'd like to get rid of the conditional for a but can't find a way to make it shorter. \$\endgroup\$ Feb 21 at 22:34
4
+100
\$\begingroup\$

Vyxal, 89 bytes

d3+\-*,λ\|?n*J\|Jʁ,;→8*‹(ð←†)\-←†\\?d›IJ\/J,ð\\J?d‹\-*J\/J,?‹(n›I\\J?nd-?‹+IJ\/J,)?›I\vJ,

Try it Online!

Explanation

d3+\-*,    Print 2n + 3 dashes

λ                Lambda to print:
 \|              Pipe joined with
   ?n*J;         A string (arg) n times joined with
        \|J      Another pipe
            ʁ,→  Palindromised (and set lambda to anon. var)

8*‹(ð←†)         Loop 8*n - 1 times, call the lambda passing a space as arg
\-←†             Call same lambda with dash as arg
\\?d›IJ\/J,      Print \, n*2+1 spaces, and /
ð\\J?d‹\-*J\/J,  Print space, \, n*2-1 dashes, and /


?‹(n›I\\J?nd-?‹+IJ\/J,)    Loop through n-1 with loop index i
   n›I\\J                  Pad n+1 spaces and /
         ?nd-?‹+IJ         Append i-n*2+i-1 spaces
                  \/J,     Append \/ and print

?›I\vJ,                    Print v padded with n+1 spaces

\$\endgroup\$
1
  • \$\begingroup\$ Bug was patched a while ago \$\endgroup\$
    – emanresu A
    May 26 at 23:11
3
\$\begingroup\$

JavaScript (Node.js), 304 bytes 296 bytes

x=n=>{p="repeat",u=1,h="\\",o=["|","|","|"],r=o.join(" "[p](n))+"\n",s=o.join("_"[p](n)),e=["-"[p](2*n+3)];for(e.push(r[p](8*n-1)+s),e.push([h,"/"].join(" "[p](2*n+1))),e.push(" "+h+"-"[p](2*n-1)+"/");u<n;)e.push(" "[p](1+u)+h+" "[p](2*n-2*u++-1)+"/");return e.push(" "[p](n+1)+"V"),e.join("\n")}

Try it online!

This is my first try at code golfing so it's a very simplistic attempt. The long version would be:

var x=(n) => {
  // Set some utility variables
  let i = 1
  let space = ' '
  let backslash = '\\'
  let repeat = 'repeat'
  let pipes = ['|', '|', '|']
  let segment = pipes.join(space[repeat](n)) + '\n'
  let lastsegment = pipes.join("_"[repeat](n))
  
  // Create the first line of dashes, the body with the last segment and the first line of the tip
  let pencil = ["-"[repeat](n * 2 + 3), segment[repeat](8 * n - 1) + lastsegment, [backslash, "/"].join(space[repeat](n * 2 + 1))]

  // Create the dashed tip line and the rest of the empty tip in a loop
  for(pencil.push(space+backslash+"-"[repeat](2*n-1)+"/");i<n;){
    pencil.push(space[repeat](1+i)+backslash+space[repeat](2*n-2*i++-1)+"/")
  }

  // Add the tip, join the array and return
  pencil.push(space[repeat](n + 1) + "V")
  return pencil.join("\n")
}
\$\endgroup\$
7
  • 3
    \$\begingroup\$ Hi, welcome to Code Golf, and great first answer! \$\endgroup\$
    – mathcat
    Feb 22 at 13:14
  • 2
    \$\begingroup\$ Although I'm not that familiar with Javascript, I think you can remove var and let. Try it Online! \$\endgroup\$
    – mathcat
    Feb 22 at 13:18
  • \$\begingroup\$ great @mathcat, less than 300 bytes! \$\endgroup\$ Feb 22 at 13:20
  • \$\begingroup\$ -11 bytes \$\endgroup\$
    – mathcat
    Feb 22 at 14:20
  • 1
    \$\begingroup\$ @hypers yeah, I used dashes for the top instead of underscores, my bad. Allthough it does not alter the result or add/save any bytes. \$\endgroup\$ Feb 23 at 16:03
3
\$\begingroup\$

C (gcc), 211 bytes

#define F(N,I)for(I=N;I>0;I--)
#define P(C)putchar(C);
i,j;main(c,v){F(2*c+3,i)P(95)P(10)F(8*c,i){F(2,v){P('|')F(c,j)P(i<2?95:32)}P('|')P(10)}F(c+1,i){P(92)F(2*i-1,j)P(i^c?32:45)P(47)P(10)F(c-i+2,j)P(32)}P('v')}

Try it online!

This is my first time code golfing.

The number of arguments is used as the input, though the value of those arguments is irrelevant. In the example on Try it online, 2 additional arguments are passed (in addition to the program name itself) so here, n=3.

The main obfuscations used are

  • define macros for printing characters and for loops
  • replacing character literals with their decimal encoding (where it saved characters)
  • the use of main()'s argument variables for temporary storage
  • and, naturally, the removal of white-space.

I decided to have all for loops run backwards as it ends up using slightly fewer symbols overall due to the loop used for aligning the tip of the pencil.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Welcome to Code Golf, and nice first answer! \$\endgroup\$
    – pxeger
    Feb 22 at 20:19
  • \$\begingroup\$ You can save 3 bytes by just defining a function rather than a full program with main: Try it online!. (This also uses a less strange input method!) \$\endgroup\$
    – pxeger
    Feb 22 at 20:32
  • \$\begingroup\$ You can also remove the >0 in the for-loop, because the value of I itself is good enough as a condition. \$\endgroup\$
    – pxeger
    Feb 22 at 20:34
  • \$\begingroup\$ Ah nice! I didn't realise that was allowed, I assumed it had to compile into a standalone binary. \$\endgroup\$
    – Ali
    Feb 22 at 20:35
  • \$\begingroup\$ You can see all the rules for what input/output formats are allowed here: codegolf.meta.stackexchange.com/questions/2447/… \$\endgroup\$
    – pxeger
    Feb 22 at 20:55
2
\$\begingroup\$

Python 3.8 (pre-release), 155 bytes

lambda n:'_'*(2*n+3)+f'\n|{(" "*n+"|")*2}'*(8*n-1)+f'\n|{("_"*n+"|")*2}\n'+'\n'.join(' '*i+('v'*(i>n)or f'\\{" -"[i==1]*(2*n-2*i+1)}/')for i in range(n+2))

Try it online!

Sorry... can't shorten no more.

\$\endgroup\$
2
\$\begingroup\$

PowerShell, 134 bytes

inspired by Zaelin Goodman.

param($n),'__'+,'| '*(8*$n-1)+'|_'|%{,$_[0]*3-join("$($_[1])"*$n)}
0..$n|%{' '*$_+"\$((' ','-')[$_-eq1]*(2*$n+1-2*$_))/"}
' '*++$n+'v'

Try it online!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 143 bytes

n=>'_'[R='repeat'](w=n*2+3)+(g=x=>x<0?`
|${s=' _'[+!++x][R](n)}|${s}|`+g(x):`
`+' '[R](x)+(x>n?'v':`\\${' -'[+!~-x][R](w-++x*2)}/`+g(x)))(-8*n)

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C++ (gcc), 252 245 241 bytes

#import<iostream>
#define g(i,n)for(int i=0;i<n;++i)
#define c std::cout<<
int f(int n){g(i,2*n+3)c'_';c'\n';g(i,8*n){g(j,2){c'|';g(k,n)c" _"[i==8*n-1];}c"|\n";}g(i,n+2){g(j,i)c' ';if(i+~n){c'\\';g(j,n-~n-i-i)c" -"[i==1];c"/\n";}else c'v';}}

Try it online!

  • 7 bytes saved thanks to ceilingcat
  • 4 bytes saved thanks to ceilingcat
\$\endgroup\$
0
1
\$\begingroup\$

JavaScript (Node.js), 205 bytes

n=>[(g=([[f,...d]])=>d.join((r=(n,c=' ')=>c.repeat(n+!~n))(n,f)))`____`,r(8*n-1,g` |||
`)+g`_|||`,g` \\ /`,(i=0,G=([[b,e,f]])=>r(++i)+b+r(2*(n-i)+1,f)+e)`\\/-`,...Array(n-1).fill`\\/`.map(G),G`v `].join`
`

Try it online!

The core of this solution lies in the abuse of template parameters and tags in some internal functions:

  • r(n,c) is just c.repeat(n), except n changes to 0 if it's -1, and c defaults to a space.
  • g([[f,...d]]) joins d with r(n,f).
  • G([[b,e,f]]) is similar to g but adds increasing padding on each iteration (for the pencil tip).

ES6 destructuring in function parameters allows for the use of template strings in place of comma separated character arguments, like this:

const f1 = (a,b,c) => [c,b+a];
const f2 = ([[a,b,c]]) => [c,b+a];
console.log(f1('a','b','c')) // -> ['c', 'ba']
console.log(f2`abc`)         // -> ['c', 'ba']
\$\endgroup\$
1
\$\begingroup\$

PowerShell (Version <=6), 139 bytes

param($n)'_'*(2*$n+3)
,($b=('|'+' '*$n)*2+'|')*(8*$n-1)
$b|% *ce ' '"_"
0..$n|%{' '*$_+"\$((' ','-')[$_-eq1]*(2*$n+1-2*$_))/"}
' '*++$n+'v'

Try it online!

PowerShell (Version >=7), 140 bytes

param($n)'_'*(2*$n+3)
,($b=('|'+' '*$n)*2+'|')*(8*$n-1)
$b|% *ace ' '"_"
0..$n|%{' '*$_+"\$((' ','-')[$_-eq1]*(2*$n+1-2*$_))/"}
' '*++$n+'v'

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ you can save 1 byte if you omit the line break after param. \$\endgroup\$
    – mazzy
    Feb 22 at 16:57
  • \$\begingroup\$ 'Input name "*ce" is ambiguous' with PS7.2. Please use *ace. I'm sorry. \$\endgroup\$
    – mazzy
    Feb 22 at 17:56
  • \$\begingroup\$ nice ' '"_" for 2 parameters! \$\endgroup\$
    – mazzy
    Feb 22 at 18:01
1
\$\begingroup\$

Perl 5 + -M5.10.0 -n, 127 bytes

//;say for"_"x($'*2+3),($_=join$"x$_,qw(|)x3)x(8*$'-1),y/ /_/r,(map$"x$_.v92.($_-1?$":'-')x(2*($'-$_)+1)."/",0..$')
,$"x$'." v"

Try it online!

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

Vyxal, 40 37 bytes

⇧\_*\|?꘍Ǐ?8*Ḋ\_Ḟ\\?›꘍¦Ṙ1‡\-Ḟ¨M÷_\vWøṗ

Try it Online!

How?

⇧\_*\|?꘍Ǐ?8*Ḋ\_Ḟ\\?›꘍¦Ṙ1w‡\-Ḟ¨M÷_\vWøṗ
⇧                                       # Push input + 2
 \_*                                    # Push that many underscores
    \|                                  # Push "|"
      ?꘍                                # Append the input amount of spaces
        Ǐ                               # Append its own head, aka "|"
         ?8*                            # Push input * 8
            Ḋ                           # Duplicate the string that many times
             \_Ḟ                        # On the top string, replace all spaces with "_"
                \\                      # Push a backslash
                  ?›꘍                   # Append input+1 spaces
                     ¦                  # Pop and push its prefixes
                      Ṙ                 # Reverse
                       1‡\-Ḟ¨M          # On the second string, replace all spaces with a hyphen
                              ÷_        # Push each item of that to the stack, and discard the top one (a backslash)
                                \v      # Push "v"
                                  W     # Wrap everything on the stack into a list
                                   øṗ   # Palindromise everything, center, and join by newlines

-2 bytes thanks to emanresu A

\$\endgroup\$
1
  • \$\begingroup\$ Not 35, but string prefixes = cumulative sums \$\endgroup\$
    – emanresu A
    May 27 at 9:10

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.