New answers tagged

1

05AB1E, 86 81 77 67 bytes Î×2.ø$×.ø©DSdJD.ΓN>·.$}`r[D¬≠#NÌF¦¨]\\0'..;R®1K¹;.D®TS'_:).c2Ý… @|‡ Try it online! -5 -9 -19 @ Kevin Cruijssen, thanks! This is some unorthodox stuff, definitely not my best answer... It's been awhile, I'm rusty. Also tried to use some of the new commands like Δ which did not go as planned, and I had to use a global register. ...


1

Charcoal, fixed height: 10 bytes G↗↘←⁹0M⁹↑* Try it online (verbose) or try it online (pure). Explanation: Draw a polygon in the shape of a triangle, with the initial arm as size 9 and using the character "0" as filler: Polygon(:UpRight, :DownRight, :Left, 9, "0"); G↗↘←⁹0 Move the cursor up 9 times: Move(9, :Up); M⁹↑ And print a "*" at that position: ...


0

05AB1E, fixed height: 11 bytes ¾17ÅÉ×'*š.c Try it online. Variable height through input: 12 bytes: ·ÍÅÉÅ0J'*š.c Try it online. Explanation: ¾ # Push 0 (push the counter_variable, which is 0 by default) 17ÅÉ # Push a list of the positive odd numbers <= 17: [1,3,5,7,9,11,13,15,17] × # Repeat the 0 that many times as ...


0

Erlang (escript), 134 bytes The tree is resizable. r(-1,_)->"";r(X,Y)->lists:duplicate(X," ")++lists:duplicate(Y,"0")++"\n"++r(X-1,Y+2). t(X)->lists:duplicate(X-1," ")++"*\n"++r(X-1,1). Try it online!


0

JavaScript (Babel Node), 65 bytes f=(n,s=`\\ `,l)=>n?'|'+s+f(n-1,' '+s,l||'-'.repeat(n&&n+1)):l||'' Try it online!


1

BATCH, 96 90 83 bytes Fixed height=10: @set s= 0 @echo%s:0=*% :a @echo%s% @set s=%s:~1%00 @if %s:~0,1%.==. goto a Saved 6 bytes by replacing \r\n line terminators with \n (still works) P.S I'm new to golf, so I'm not sure if pause is required. Removed @pause, saved 7 bytes


8

Java 11, 282 273 bytes n->{String r="",y="_".repeat(n),e="|",N="\n",S=" ",q=S.repeat(n-1),z;int i=1,w=0,s=3*n+2;for(;s>1;s-=2*i,w+=i++)r=S.repeat(w)+"@".repeat(s)+N+r;for(i=1,s=n;s>0;s-=i)r+=S.repeat(n-s)+(z="@".repeat(s))+e+q+(s>++i?S:".")+e+z+N;return r+((q+=S+e)+q+N).repeat(n-i+1)+y+e+y+e+y;} Port of @Noodle9's Python answer, after I helped ...


18

JavaScript (ES8),  215 ... 199  197 bytes f=(n,k=(W=3*n+2)**.5-.5|0,x=.5-k-(W-n)**.5,R=(n,k)=>S=''.padEnd(n,'.@_'[k]))=>k+n+2?(w=k*-~k/2,k<0?(x>0?s=R(n,k+n+3):R(w+=~k,s=R(n-1)+R(1,~~x))+R(n-w,1))+`|${s}|`+S:R(w)+R(W-w*2,1))+` `+f(n,k-1):'' Try it online! or Test it online! against an ungolfed, straightforward implementation How? We ...


11

Python 3, 249 \$\cdots\$ 211 210 bytes Saved 6 \$\cdots\$ 17 18 bytes thanks to Kevin Cruijssen!!! def f(n): i=j=1 t,y,e,w,s,x,S=[],n*'_','|',0,3*n+2,n*' ',n while s>1:t=[w*' '+s*'@']+t;s-=2*i;w+=i;i+=1 while S>0:j+=1;t+=[x[S:]+S*'@'+e+x[1:]+'. '[S>j]+e+S*'@'];S-=j return t+[(x+e)*2]*(n-j+1)+[(y+e)*2+y] Try it online! Before golfing def ...


7

05AB1E, 106 105 103 99 97 95 93 92 bytes $×0I>;úR©«D1s∍ILηOD¦<‚vyvDðyL<ǝ}r}r)ʒ1å}`'.I>ǝ)DJ0¢Iα®иIú0'_I×쬮¦∍«)˜IÉi.ºëº}»T„@|‡I≠i'.ð.; Try it online or verify the first 10 test cases. Explanation: $ # Push 1 and the input-integer × # Pop both, and push a string consisting of the input amount of "1"s 0 ...


9

Charcoal, 78 71 bytes Nθ≔×¹·⁵⊕θηW›ηⅈ«↑P×⁻ηⅈ@Mⅉ←»J⁰θ×θ_P×⊘⁺³θ_↑⊕θ⸿W›θⅈ«P×⁻θⅈ@M⁺²ⅉ¹»Jθⅉ↗‖OO﹪θ². Try it online! Link is to verbose version of code. Explanation: Nθ Input n. ≔×¹·⁵⊕θη Calculate the half width of the reference row. W›ηⅈ« While there is still canopy left to print, ... ↑ ... move up a line, ... P×⁻ηⅈ@ ... print some canopy, ... Mⅉ←» ......


0

Python 3, 91 86 bytes n=range(int(input())) for i in n:print(' '*i+'\\') for i in n:print(' '*(n[-1]-i)+'/') Try it online! Edit 1: saved 5 bytes by making n a range instead of integer


1

Erlang (escript), 69 bytes Port of xnor's Python answer. r(0,_)->"";r(I,X)->X++"\\\n"++r(I-1,X++" ")++X++"/\n". r(I)->r(I,""). Try it online!


0

x86 machine code, 155 bytes Hexdump: 60 8b fa 51 b0 20 f3 aa 59 51 b0 5f 03 c9 f3 aa 5e b0 0a aa 57 5f 57 b7 2f 8d 14 36 8b ce 2b ca 75 03 80 f7 73 7d 02 f7 d1 51 b0 20 f3 aa 59 8a c7 aa 8b ee 53 6b d9 fe 8d 5c b3 ff 53 6b de fc 8b 5c 1f fe 8b 47 fe 80 fc 5f 74 23 8a c4 34 73 3c 2f 75 0a 80 ff 5f 74 3e 80 ff 5c 74 37 c1 eb 08 66 81 fb 2f 5c 74 2f 0f c7 f3 ...


1

C++ (gcc), 65 bytes #include<ios> int f(int n){for(n&&f(n-1);~n;putchar(n--?42:10));} Including the #include in the byte count. Try it online!


1

Charcoal, 33 bytes WS⊞υ⪫⪪ι ψP⪫υ¶F⊟υ¿⊖№KD²↓|KK«¤\↓¤\↗ Try it online! Link is to verbose version of code. Expects rectangular input with newline terminator. Explanation: WS⊞υ⪫⪪ι ψ Read the input but change spaces into nulls. P⪫υ¶ Copy the input to the output; nulls turn into fillable spaces. F⊟υ Loop over the width of the input. ¿⊖№KD²↓| Is this a ...


3

05AB1E, 25 24 23 bytes '_'|7úD)³∍ε73N3÷è¹-._²∍ -1 byte thanks to @Grimmy. Inputs in the order \$\text{offset}, \text{width}, \text{height}\$. Output as a list of string-lines. Try it online or verify all test cases. Explanation: '_ '# Push "_" '| '# Push "|" 7ú # Pad 7 leading spaces: " |" D # Duplicate ...


2

Since there's a pandemic out there, I thought the world could use one more thermometer: Perl 5, 180 bytes say$_=".-----------. ",y/.-/| /r x2,"'-. .-' .-' - '-.";map{printf"|%5.1f-%5.1f| ",32+$_*.9,$_/2}reverse 70..84;say"'---. - .---' | - | |---| | | '---'" Try it online!


1

JavaScript (ES6),  81 78  77 bytes Saved 3 bytes thanks to @mypronounismonicareinstate Takes input as (F, W)(H). (F,W,x=y=0)=>g=H=>y<H?` |_ `[x++<W?y%3?(x+~F^y/3<<2)&7?3:1:2:x=+!++y]+g(H):'' Try it online!


2

Bash + Core utilities, 154 152 145 142 136 119 bytes IFS=\ read t d=`sed s/././g<<<$t` sed -E ":l;s/([|%]$d|%) /\1%/;tl;s/ ($d[|%]|%)/%\1/;tl;"'y/%@/\\\ /'<<<$t@`tr ' ' @` Try it online! Input on stdin, and output on stdout. (The script has spurious output to stderr in one case, but that's considered acceptable.) Here are the outputs ...


2

Ruby, 144 bytes ->s{s.map{|l|((r=s[0]).size.times.map{|i|r[i]!=s[1][i]?[r.rindex(?|,i-1),r.index(?|,i+1)]:p}-[p]).map{|a,b|a.upto(b){|i|l[i]=?\\if l[i]<?!}};l}} Try it online! I thought I would try a different approach to the automaton. This finds lane dividers by checking for which indices the first two rows differ. It then goes through each of ...


3

Retina 0.8.2, 79 bytes +m`(?<=(.)+) ((?<=(?(1).)^(?<-1>.)+\S.*¶.+|\\ )|(?=\\|.*¶(?<-1>.)+(?(1)$)\S)) \ Try it online! Same flood fill approach that @WheatWizard uses in his answers but using .NET balancing groups to detect vertical matches. Would save two bytes if the wind was blowing the other direction. Explanation: +m` Repeatedly ...


5

Perl 5 -p, 52 51 bytes / /;s/(\S(.{@{-}})|\\)\K | (?=(?2)\S|\\)/\\/s&&redo Replace space character with a backslash if there is a non-space character above or below it or a backslash on the right or the left of it. And redo while no more space can be replaced. One byte saved diverting recursive regex thanks to Grimmy. Try it online!


8

Jelly,  18  16 bytes ZḢ”\E?€ṣ”|»Ṁ$€K» A monadic Link accepting a list of lines, each of which is a list of characters, which yields another, similar list. Try it online! Or see the test-suite. How? ZḢ”\E?€ṣ”|»Ṁ$€K» - Link: list of lists of characters (i.e. rows of text) Z - transpose - i.e. columns (each being: all spaces; all ...


0

Japt -R, 10 bytes õ_s*iS)êÃû Try it õ_s*iS)êÃû :Implicit input of integer U õ :Range [1,U] _ :Map s : Convert to string in base *i : "*" prepended with S : Space ) : End base conversion ê : Palindromise à :End map û :Centre pad ...


4

Jelly, 25 bytes Zn⁶ṣoFẸḤƊ$€jW} ç1€ị“|\ ”Z Try it online! I/O is a list of strings Explanation Zn⁶ṣoFẸḤƊ$€jW} Primary Link (dyad); takes the input on the left and [1, 1, ..., 1] on the right Z zip the input into a list of columns n⁶ vectorized inequality to space; "|" becomes 1, " " becomes 0 ṣ split (on [1, ...


4

Haskell, 151 bytes a#b=a!!mod b(length a) z=zip[0..] s%_=[[(['\\'|b<'!'&&(s#(x-1)#y>'['||elem '\\'[a#(y-1),a#(y+1)])]++[b])!!0|(y,b)<-z a]|(x,a)<-z s] g x=foldl(%)x(x!!0) Try it online! Explanation This implements the same algorithm as my python answer. It implements a simple automaton. At every step a space is replaced with an \ iff ...


4

Jelly, 25 bytes n⁶Zṣ1€oFẸḤƊ$€j1€W$Zị“|\ ” Try it online! oFẸḤƊ$€ is "borrowed" from @Riolku lol go upvote him Explanation n⁶Zṣ1€oFẸḤƊ$€j1€W$Zị“|\ ” Main Link n⁶ vectorized inequality to space; "|" becomes 1, " " becomes 0 Z transpose into a list of columns ṣ1€ split by [1, 1, ..., ...


4

Python 2, 112 109 107 bytes lambda r:reduce(lambda r,c:[''.join(l[::-1]).replace(c+' ',c+'\\')for l in zip(*r)],r'|\|\\\\:'*len(`r`),r) Try it online! Python 3.8 (pre-release), 109 bytes Thanks to Kevin Cruijssen for this version! lambda r:[(r:=[''.join(l[::-1]).replace(c+' ',c+'\\')for l in zip(*r)])for c in r'|\|\\\\:'*len(repr(r))][-1] Try it online!...


5

Python 3, 198 196 186 159 bytes def g(x): for _ in x[0]:x=[''.join([q,'\\'][q<'!'and(x[a-1][b]>'['or'\\'in r[b-1]+r[-~b%len(r)])]for(b,q)in enumerate(x[a]))for(a,r)in enumerate(x)] return x Try it online! Explanation This implements a simple automaton. At every step a space is replaced with an \ iff one of the following is true There is a | or \...


5

05AB1E, 20 bytes ₆F€SøíJ„ \S„\|N_èì`: Try it online! ₆F # repeat the following 36 times: €SøíJ # rotate 90 degrees „ \S # character array [' ', '\'] „\| # string "\|" N # 0-based iteration count _ # logical not: 0 => 1, anything else => ...


6

JavaScript (ES6),  118 ... 105  103 bytes I/O format: matrix of characters. a=>a.map(r=>r.map((c,i)=>(a[k=0][i]+a[1][i]<'||'?r:r=a.some(r=>r[a[k^=1].indexOf(c,i+1)]<c))/c?'\\':c)) Try it online! How? To detect lines, we test whether the first and the second row both contain a | at a given position \$i\$: a[0][i] + a[1][i] &...


2

Raku, 54 bytes {my@a;{@a[$^a..$^b;$^c..$^d]X+^=1}for $_;@a >>~|>>' '} Try it online! Takes input as a flat list of coordinates as inclusive coordinates, i.e. x1,y1,x2,y2,x1,y1,x2,y2... and outputs as a list of list of characters with k being 1 and l being 0. Explanation: { } # Anonymous ...


1

Jelly, 43 bytes +µ>2Ḥạ ạ1ẋ$0ẋ⁸¤;µ/€«þ/µ€z0z€0Z€Zz€0Z€ç"/o⁶Y Try it online! Explanation +µ>2Ḥạ Helper Link; combines final rectangles (0 is blank, 1 is covered, 2 is uncovered) + add the two values µ (with the sum...) >2 ...


1

Powershell, 24 bytes 1.."$args"|%{$a+="*";$a}


1

Erlang (escript), 50 bytes s(0)->"";s(N)->s(N-1)++string:copies("*",N)++"~n". Try it online! Explanation s(0)->""; % If the current item is 0, return the empty string. s(N)-> % Otherwise: s(N-1) % Return the N-1 string. ++string:copies("*",N). % Append "*" N times. ++"~n" % Append a newline.


1

1987 - Erlang The testing version of Erlang was released to the public on 1987. Erlang is a multipurpose programming language designed for concurrency and distributed systems. It is designed to be easy to learn and use, and it's special for its error-tolerance. Task 1 main([]) -> io:fwrite("Erlang was made in 1987!"). % Just a boring fwrite of the ...


1

Keg, -pn, 8 bytes Ï^⑷`*`*, Try it online! Explanation Ï^ In the first part of the program, we push a range from 0 to n onto the stack. Then we reverse it because otherwise, the triangle is upside down. ⑷ We then start a mapping to the entire stack with the following: `*`*, Which says to multiply the string * by the number being mapped and print ...


2

C++ (gcc), 61 bytes int f(int n){for(n&&f(n-1);~n;__builtin_putchar(n--?42:10));} Uses ASCII char codes and builtins to shorten another solution. Try it online!


4

brainfuck, 168 bytes ,[->>>+<[-<+>>-]>[<+>->]<<<<]>>>>>>>++>++[-<[-<++>]<[->++<]>>]<<++++++[-<+++++++>]<[-<+<+++>>]++++++++++<+++++<--<[->.>>.<<<]<[[->+>>>>>.<<<<<<]>>&...


0

Go, 94 bytes import."fmt" func f(n int){for n!=0{if n%2==0{Printf("%*s/\n",n/2,"")}else{Println("|")} n--}} Port of my C answer. Try it online!


1

C (gcc), 50 bytes f(n){for(;n;)printf(n--%2?"|\n":"%*s/\n",n/2,"");} Takes as input an integer n and prints a triangle. It would look more realistic and save 2 bytes if n was not divided by 2. Try it online!


2

4, 171 bytes 3.6006260132602476031061101612026401020000127908906554819090552999940099999079098993139912214131211599141161115815500503615009816317991281750111717119502503616009199991194 Try it online! Explanations 3. start of code 60062 assign half ASCII of pipe 60132 space 60247 slash 60310 endl 61101 int one 61202 int two 64010 int ten 2000012 ...


2

Java (OpenJDK 8), 88 84 bytes x->{for(int i=x,j=i+2;i-->0;j--)System.out.printf((i%2<1?"|\n":"%"+j/2+"s\n"),"/");} Try it online! If Java's output methods weren't so long, I'd get close to John's R submission! Edit: -4 bytes thanks to @Kevin Cruijssen! Also the link led to an older 91 bytes version, fixed that now...


1

R, 75 61 bytes Thanks again, Giuseppe! Forgot that cat does pasting itself. for(i in scan():1)cat(`if`(i%%2,'|',c(rep('',i/2),'/')),'\n') Try it online!


2

PowerShell, 42 bytes "$args"..1|%{((" "*($_/2)+"/"),"|")[$_%2]} Try it online! Loops from input $args down to 1, each iteration choosing whether to output spaces and a /, or the | character based on a modulo-index into an array. Default output gives us newlines for free. Ho-hum.


0

Bash, 68 bytes f(){ for((i=$1;i-->0;)){((i%2))&&printf %$[2+i/2]s\\n /||echo \|;};} Try it online! Don't golf in bash that often, so tips appreciated.


2

Jelly, 14 13 bytes H⁶ṁ;”/)”|ÐoṚY Try it online! A monadic link taking an integer and returning a list of Jelly strings. If a single newline-separates string is needed, append Y for a cost of a byte. Thanks to @JonathanAllan for saving a byte!


0

JavaScript (Node.js), 47 bytes f=(n,s=`/ `,v=`| `)=>--n?f(n,v,s<v?' '+s:s)+v:v Try it online!


0

Ruby, 44 bytes ->n{(1..n).map{[?|," "*(n/2)+?/][(n-=1)%2]}} Try it online!


Top 50 recent answers are included