# Draw an ASCII pencil

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.

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

# Charcoal, 30 bytes

Ｎθ×_θ↑×⁸θ←×_⊕θ↓_↓×⁸θ↘¹Ｐ⊕θ↘θv‖Ｂ


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

Ｎθ


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.

Ｐ⊕θ


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.

‖Ｂ


Reflect to complete the pencil.

# 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! # 05AB1E, 44 bytes Ì'_×'|IúRĆI8*.Dð'_:'\I>úRηRεNið'-:]\'v).º.c  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)  # 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. • Welcome to the site, and nice first answer! Feb 27 at 15:10 # 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! • 116 bytes Feb 21 at 13:01 • @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. Feb 21 at 22:34 # 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  • Bug was patched a while ago May 26 at 23:11 # 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") }  • Hi, welcome to Code Golf, and great first answer! Feb 22 at 13:14 • Although I'm not that familiar with Javascript, I think you can remove var and let. Try it Online! Feb 22 at 13:18 • great @mathcat, less than 300 bytes! Feb 22 at 13:20 • -11 bytes Feb 22 at 14:20 • @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. Feb 23 at 16:03 # 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. • Welcome to Code Golf, and nice first answer! Feb 22 at 20:19 • 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!) Feb 22 at 20:32 • You can also remove the >0 in the for-loop, because the value of I itself is good enough as a condition. Feb 22 at 20:34 • Ah nice! I didn't realise that was allowed, I assumed it had to compile into a standalone binary. – Ali Feb 22 at 20:35 • You can see all the rules for what input/output formats are allowed here: codegolf.meta.stackexchange.com/questions/2447/… Feb 22 at 20:55 # 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. # 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!

# 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! # C++ (gcc), 252245 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 # 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),Gv ].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(f2abc) // -> ['c', 'ba']  # 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*$_))/"}

Try it online!

# 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

• Not 35, but string prefixes = cumulative sums May 27 at 9:10