| Bytes | Lang | Time | Link |
|---|---|---|---|
| 103 | Tcl | 170712T012102Z | sergiol |
| 090 | Python 3 | 211030T183302Z | python-b |
| 064 | PowerShell | 200227T124920Z | mazzy |
| 080 | C gcc | 170711T131159Z | user4180 |
| 019 | 05AB1E | 181217T183649Z | Kevin Cr |
| 075 | C gcc | 181217T170246Z | l4m2 |
| 022 | MathGolf | 181217T163925Z | maxb |
| 079 | Perl 5 | 171219T235802Z | Xcali |
| 067 | R | 170714T102646Z | Leaky Nu |
| 113 | Java 8 | 171103T094306Z | Kevin Cr |
| 054 | Octave | 170712T061935Z | Michthan |
| 193 | Lua | 170721T085635Z | AI221 |
| 090 | Bubblegum | 170720T180303Z | ovs |
| 065 | WendyScript | 170712T234511Z | Felix Gu |
| 231 | /// | 170712T200101Z | Conor O& |
| 091 | Python 2.7 | 170711T133718Z | Koishore |
| 036 | Paintbrush | 170712T170535Z | hyperneu |
| 087 | JavaScript ES6 | 170712T014034Z | darrylye |
| 032 | Japt | 170711T223226Z | Justin M |
| 016 | Jelly | 170711T170717Z | Dennis |
| 059 | Vim | 170711T151314Z | biowease |
| 018 | Jelly | 170711T155934Z | Leaky Nu |
| 015 | Charcoal | 170711T152823Z | Okx |
| 030 | V | 170711T155240Z | nmjcman1 |
| 022 | Pyth | 170711T154420Z | Leaky Nu |
| 093 | Bubblegum | 170711T153538Z | musicman |
| 075 | Mathematica | 170711T140403Z | Not a tr |
| 165 | Positron | 170711T135526Z | hyperneu |
Tcl, 103 bytes
time {incr j
time {puts -nonewline [expr 21-abs(abs([incr i]-31)-10)-$j?"|":"~"]} 61
puts [unset i]} 20
time {incr j
set i 0
time {puts -nonewline [expr 21-abs(abs([incr i]-31)-10)-$j?"|":"~"]} 61
puts ""} 20
time {incr j
set i 0
time {puts -nonewline [expr $j==21-abs(abs([incr i]-31)-10)?"~":"|"]} 61
puts ""} 20
time {incr j;set i 0;time {append s [expr $j==21-abs(abs([incr i]-31)-10)?"~":"|"]} 61;set s $s\n} 20
puts $s
#Tcl, 143 133 123 110
Still much ungolfed, but I will evolve it after:
time {incr j;set i 0;time {incr i;append s [expr $j==21-abs(abs($i-31)-10)?"~":"|"]} 61;set s $s\n} 20
puts $s
#demo
Python 3, 90 bytes
1 byte smaller than the other Python answer :)
Outputs with a trailing newline.
for y in range(20):print(''.join('~'if 20-y==abs(10-abs(x))else'|'for x in range(-30,31)))
PowerShell, 64 bytes
0..19|%{$y=$_
-join(0..20+19..10+11..19+20..0|%{'|~'[$_-eq$y]})}
The four-part diagram with a linear function y=x
C (gcc), 97 82 81 80 bytes
Golfed 15 bytes after learning that abs is a builtin in C, an additional byte thanks to Rogem for pointing out that the declarations of my variables can be moved to the function, and another byte thanks to ceilingcat for suggesting x=31;--x+31 instead of x=-31;++x<31.
f(x,y){for(y=21;--y;puts(""))for(x=31;--x+31;)printf(abs(10-abs(x))-y?"|":"~");}
This outputs with a trailing newline. The function f does the outputting.
Explanation
The output can be stated as a graph.
~|||||||||||||||||||||||||||||+|||||||||||||||||||||||||||||~
|~||||||||||||||||||||||||||||+||||||||||||||||||||||||||||~|
||~|||||||||||||||||||||||||||+|||||||||||||||||||||||||||~||
|||~||||||||||||||||||||||||||+||||||||||||||||||||||||||~|||
||||~|||||||||||||||||||||||||+|||||||||||||||||||||||||~||||
|||||~||||||||||||||||||||||||+||||||||||||||||||||||||~|||||
||||||~|||||||||||||||||||||||+|||||||||||||||||||||||~||||||
|||||||~||||||||||||||||||||||+||||||||||||||||||||||~|||||||
||||||||~|||||||||||||||||||||+|||||||||||||||||||||~||||||||
|||||||||~||||||||||||||||||||+||||||||||||||||||||~|||||||||
||||||||||~|||||||||||||||||||+|||||||||||||||||||~||||||||||
|||||||||||~|||||||||||||||||~+~|||||||||||||||||~|||||||||||
||||||||||||~|||||||||||||||~|+|~|||||||||||||||~||||||||||||
|||||||||||||~|||||||||||||~||+||~|||||||||||||~|||||||||||||
||||||||||||||~|||||||||||~|||+|||~|||||||||||~||||||||||||||
|||||||||||||||~|||||||||~||||+||||~|||||||||~|||||||||||||||
||||||||||||||||~|||||||~|||||+|||||~|||||||~||||||||||||||||
|||||||||||||||||~|||||~||||||+||||||~|||||~|||||||||||||||||
||||||||||||||||||~|||~|||||||+|||||||~|||~||||||||||||||||||
|||||||||||||||||||~|~|||||||||||||||||~|~|||||||||||||||||||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
(The +s are only shown for explanation purposes and represent the axes.)
The equation of this graph is \$y=\text{abs}(10-\text{abs}(x))\$ as can be seen here in this link to a Desmos graph.
abs(10 - abs(x))
abs( ) Reflect whatever is beneath the x-axis to above the x-axis
10 - abs(x) This forms the central triangle-like structure
In function f, we have two for-loops that iterate through every coordinate in this graph. y goes from 20 to 1 and x goes from -30 to 30.
For every x, we check if abs(10-abs(x)) equals y by doing abs(10-abs(x))-y in a ternary. If they are equal, this yields 0, a falsey value in C, otherwise it will evaluate to some positive value. Then in the ternary abs(10-abs(x))-y?"|":"~", we printf accordingly.
And after each line, we output a newline using puts(""), and that is how the function outputs with a trailing newline.
05AB1E, 20 19 bytes
20F„|~20Ýû31∍ûNQèJ,
Explanation:
20F # Loop 20 times:
„|~ # Push the string "|~"
20Ý # List of range [0,20]
û # Palindromize [0..20..0]
31∍ # Shorten to length 31 [0..20..10]
û # Palindromize again [0..20..10..20..0]
NQ # Check if the loop index is equal to it
è # And index it into the string
J # Then join the list of characters together
, # And print with trailing newline
20Ýû31∍û generates the list:
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,11,10,11,12,13,14,15,16,17,18,19,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0]
C (gcc), 75 bytes
f(x){for(x=1240;--x;)putchar(x%62?x/62+1-abs(10-abs(x%62-31))?124:126:10);}
Totally changed from Cows quack's answer
MathGolf, 22 bytes
I{S╒xñ♂-±Iï--mÆ┬û|~§yp
Explanation
It's probably possible to golf away 2-3 bytes from this, I'll see what I can do.
I push 20
{ start block or arbitrary length
S push 30
╒ range(1,n+1)
x reverse int/array/string
ñ pop(a), push palindromize(a) string/list/number
♂ push 10
- pop a, b : push(a-b)
± absolute value
I push 20
ï index of current loop, or length of last loop
- pop a, b : push(a-b)
- pop a, b : push(a-b)
m explicit map
Æ start block of length 5
┬ check if equal to 0
û|~ string "|~"
§ get from array/string
y join array without separator to string or number
p print with newline
Perl 5, 79 bytes
push@a,[('|')x61]for 1..21;map$a[20-abs 10-abs][$_+30]='~',-30..30;say@$_ for@a
R, 70 67 bytes
3 bytes thanks to Giuseppe.
write(c("|","~")[outer(abs(10-abs(-30:30)),20:1,"==")+1],"",61,,"")
Java 8, 113 bytes
v->{String r="";for(int i=-1,j;++i<20;r+="\n")for(j=61;j-->0;)r+=j==i|j+i==60|i>9&(j-i==20|j+i==40)?"~":"|";return r;}
I have the feeling the checks (j==i|j+i==60|i>9&(j-i==20|j+i==40) can definitely be golfed by somehow combining multiple checks into one.
Explanation:
v->{ // Method with empty unused parameters and String return-type
String r=""; // Result-String
for(int i=-1,j; // Index integers
++i<20; // Loop (1) from 0 to 20 (exclusive)
r+="\n") // After every iteration: append a new-line to the result-String
for(j=61; // Reset `j` to 61
j-->0;) // Inner loop (2) from 60 down to 0 (inclusive)
r+= // Append the result-String with:
j==i // If `j` and `i` are equal (top-right /),
|j+i==60 // or `j` + `i` is 60 (top-left \),
|i>9 // or we're at the bottom halve
&(j-i==20 // and `j` - `i` is 20 (bottom left \),
|j+i==40)? // or `j` + `i` is 40 (bottom right /)
"~" // Append a literal "~" to the result-String
: // Else:
"|"; // Append a literal "|" to the result-String
// End of inner loop (2) (implicit / single-line body)
// End of loop (1) (implicit / single-line body)
return r; // Return the result-String
} // End of method
Octave, 157 57 54 bytes
Golfed it further down, thanks to the other answers and the comments.
a=zeros(20,61);for i=-30:30;a(21-abs(10-abs(i)),i+31)=2;end
a=char(a+124)
I just approached it like the other answer with the abs(10-abs(x)) function and then used the right ASCII characters to print out the image.
Lua, 193 bytes
m={}function g(x,y)if(x<62)then
m[x+y*61]="~"if(x==31or x==21or x==41)then
b=not b
end
g(x+1,y+((b and-1)or 1))end
end
g(1,0)for y=0,19 do
s=""for x=1,61 do
s=s..(m[x+y*61]or"|")end
print(s)end
Note that Lua cannot print something out without creating a new line. For this reason, I have to break one of the rules.
Minimally minified to a large extent:
map={}
o=20
p=61
--b is true means go up
function bounce(x,y,c)
if (x~=p)then
map[x+y*p]=c
if(x==31 or x==21 or x==41)then
b=not b
end
bounce(x+1,y+((b and -1) or 1),c)
end
end
bounce(1,0,"~")
--map[2+60] = "h"
for y=0,o-1 do
str = ""
for x=1,p do
str = str..(map[x+y*p] or "|")
end
print(str)
end
Some changes where made during minification, all of which makes the program less extendable but smaller.
I'm not sure if anyone has done this before, but I tried minifying by loading the program as a string and using gsub(search/replace). Unfortainitly, it made the program bigger. However, if this program was big enough, it would yield in less bytes.
g=string.gsub
loadstring(g(g(g('m={}function g(x,y)if(x<62vm[x+y*61]="~"if(x==31zx==21zx==41vb=not beg(x+1,y+((b and-1)z1))eeg(1,0)fzy=0,19 do s=""fzx=1,61 do s=s..(m[x+y*61]z"|")eprint(s)e','e',' end '),'v',')then '),'z','or '))()
Due to its relative closeness with the real result(240 bytes, only 41 more), I figured I'd post it. If this program where 350+ bytes, there would have likely been a reduction.
Bubblegum, 90 bytes
00000000: 9dcb a10d 0040 08c5 50cf cc4d 673f 85ab .....@..P..Mg?..
00000010: b880 22fd 7972 3f07 ef98 e1cc 85e1 ca05 ..".yr?.........
00000020: 8623 97d5 78c2 abf1 8457 e305 b31a 0f78 .#..x....W.....x
00000030: f507 0fcc 54fc 6ed3 794b b6d2 c1ed 163a ....T.n.yK.....:
00000040: b8dd 42c7 68b7 d031 f757 3ab8 dd42 07b7 ..B.h..1.W:..B..
00000050: 5be8 e076 0b1d dcaf 060f [..v......
WendyScript, 65 bytes (exclude newline)
<<a=>(x)?x<0/>-x:/>x
#y:20->0{#x:-30->31?a(10-a(x))==y@"~":@"|"""}
Follows the same principle as the C answer given above.
The first line is the abs function, second line runs two for loops and outputs ~ or | based on the graph. The last "" is used to output a newline after each loop on y.
///, 231 bytes
/3/|~//2/\/\///1/!!20/|
2-/,/|#2'/"""2&/||2%/1|2#/&~2"/1!2!/&&|/~'%,
3'1#0#'1~&
,'!,&0-'!3&&
!~'-!
!3'#!0!#'~!&
!,""%#!&0!-""%~!&&
1~"-"-1
%~"#3"#%
1#"~,"~%0%#%#!~%#%&
%,%~!#%~%&0"~!-!-!-"
"3!#%~!#"0"#!~%#!~"&
",,"~,"&0"-3"#3"&&
Try it online! Or, view it interactively here!
Python 2.7, 163 138 135 133 113 91 bytes
l,t=S='|~'
for s in range(20):a=[l]*61;a[s]=a[60-s]=t;a[40-s]=a[20+s]=S[s>9];print`a`[2::5]
Edit 1: -25 bytes: changed the algorithm after I felt a bit ambitious. :P
Edit 2: -3 bytes: courtesy Felipe Nardi Batista
Edit 3: -2 bytes: courtesy shooqie
Edit 4: -20 bytes: courtesy notjagan
Edit 5: -22 bytes: courtesy Leaky Nun
Paintbrush, 36 bytes
non-competing
b|20{s~>v}10{>^s~}9{>vs~}>v20{>^s~}▁
Explanation
b|20{s~>v}10{>^s~}9{>vs~}>v20{>^s~}▁ Program
b| Sets the background character to `|`
20{ } Executes function 20 times
s~ Sets the current character to `~`
>v Moves one space right and one space down
10{ } Executes function 10 times
>^ Moves one space right and one space up
s~ Sets the current character to `~`
9{ } Executes function 9 times
>v Moves one space right and one space down
s~ Sets the current character to `~`
>v Moves one space right and one space down
20{ } Executes function 20 times
>^ Moves one space right and one space up
s~ Sets the current character to `~`
▁ Cuts off the last line (because it pads an extra line when the pointer moves near the edge)
This reminds me, I need to add a mirror operation.
JavaScript (ES6), 87 bytes
for(a=Math.abs,y=20;y--;console.log(s))for(s='',x=-30;x<31;x++)s+=a(10-a(x))+~y?'|':'~'
Japt, 32 bytes
20ç|
AÆhX'~
VméA
VpWUVmw)c ê z ·
Try it online! Be sure to expand the output box.
Explanation
20ç|
Set U to | repeated 20 times.
AÆhX'~
Set V to the range [0,9] (AÆ) mapped by:
U (implicit) with the character at index X (current value) set to (h) ~.
VméA
Set W to V with each line rotated 10 (A) chars right.
VpWUVmw
Create array: V, W, U, and V with each line reversed (w). This is now the left half of the shape, rotated 90° left.
c ê z ·
Flatten the array (c), palendromize it (ê), rotate 90° right (z), and join with newlines (·).
Jelly, 18 16 bytes
⁵×3ŒRAạ=þḤṚị⁾~|Y
⁵×3ŒRAạ=þḤṚị⁾~|Y Main link. No arguments.
⁵ Set the argument and the return value to 10.
×3 Multiply by 3 to yield 30.
ŒR Balanced range; yield [-30, -29, ..., 29, 30].
A Take absolute values.
ạ Take absolute differences with 10.
Ḥ Unhalve; yield 20.
=þ Table equals; compare each result with each k in [1, ..., 20].
Ṛ Reverse the resulting 2D array.
ị⁾~| Index into "~|", yielding '~' for 1 and '|' for 0.
Y Separate by linefeeds.
Vim, 59 Bytes
2i~^[59i|^[qqYpi|^[f~l2xA|^[q18@q11G31|qqr~jlq9@qF~2lqqr~klq8@q
Where ^[ is the <ESC> key
Bubblegum, 93 bytes
00000000: 9dcb 390e 4301 10c2 d09e 335b 9c3d 5d56 ..9.C.....3[.=]V
00000010: e72f 4c35 327a 65bf 86ee 9830 f342 5879 ./L52ze....0.BXy
00000020: 8130 f202 848d 9797 a613 262c bc7c 6a3a .0........&,.|j:
00000030: 60c2 552e 9858 bcdc a2f9 55ac 9916 5e6f `.U..X....U...^o
00000040: a285 d79b 6819 eb4d b4cc fe99 165e 6fa2 ....h..M.....^o.
00000050: 85d7 9b68 e1d5 26da 782f 3578 00 ...h..&.x/5x.
Mathematica, 78 75 bytes
Print[""<>Riffle[Array[If[#+Abs[10-Abs[31-#2]]==21,"~","|"]&,{20,61}],"\n"]]
except the \n is replaced by an actual newline. Try it online! (There are extra spaces at the starts of the lines in Mathics for some reason, but it works fine in Mathematica.)
I came up with a submission of my own, but then Kritixi Lithos added an explanation of theirs and it was quite similar to mine but using a slightly cleverer formula, so now this is just a port of that answer. (Go and read that one and upvote it!)
Positron, 165 bytes
i=0;while(i<20)do{k='|'*(59-2*i);if(i==10)then{j='|'*19;k=j+'~'+j;};if(i>10)then{q=39-2*i;j='|'*q;q=2*i-21;k=j+'~'+'|'*q+'~'+j;}print@('|'*i+'~'+k+'~'+'|'*i);i=i+1;}
I think Positron has too many bugs in it. I should get it updated to TIO because then ++ will actually work.