Embedded Triangles!

Your task: given an integer n, output an embedded triangle to the nth depth.

Standard methods of input and output, input in any convenient format, output should be as a string. Examples are 0-indexed, you can use 1-indexed if you want. Standard loopholes apply

The left and right sides of each figure is 2^n, when 0 indexed. The top is twice that.

Examples:

n=0
--
\/

n=1
----
\/ /
\/

n=2
--------
\/ /   /
\/   /
\  /
\/


This is , so shortest code wins!

This is NOT a duplicate of the Embedded Hexagons! challenge. I don't think any answers from that question could be trivially copied over to this one.

• @BetaDecay Aww, come on. same author, no big deal. Apr 15 '17 at 12:20
• Sure, but where does it end? Embedded squares? Embedded Circles? Embedded dodecagons? One challenge IMO is enough Apr 15 '17 at 12:23
• @BetaDecay How does one have an ASCII circle? More to the point, I don't think any answer to the hexagon challenge could be trivially copied over. And what is the problem with similar challenges that are substantially different? If there is one, please let me know. Apr 15 '17 at 12:26
• I'm nominating to reopen this question because I don't see how the hexagon challenge's answers could be trivially copied to this challenge. Apr 15 '17 at 14:08
• I'm voting to reopen, as I don't believe they're dupes. However, I've downvoted the challenge. It's kinda boring to have many sets of embedded <x>, and you've got at least 1 more in teh sandbox. Apr 15 '17 at 14:14

Retina, 44 bytes

^
\/
+( *)/1
$1/$1 $1/ *. - ;+(*G \\.. \  Try it online! I only want to print successful replacements, but I don't really know how to do that. As I'm no longer allowed to add another answer, here's a Batch answer for 192 bytes: @echo off set d=- set l=\ set r=/ for /l %%i in (1,1,%1)do @call set r=%%r%%%%d:-= %%/&call set d=%%d%%-%%d%% echo -%d% :l echo %l%%r% set l= %l% set r=%r:~2% if not "%r%"=="" goto l  And here's a JavaScript (ES7) port of @lanlock4's excellent Mathematica answer for 124 bytes: f= n=>[...Array(2**n+1)].map((_,i)=>[...Array(2<<n)].map((_,j)=>i?i+~j?i>j? :i+j-(i+j&-i-j)? :/:\\:-).join).join  <input type=number min=0 oninput=o.textContent=f(this.value)><pre id=o> And here's a Charcoal answer for 16 bytes: ＦＥＮＸ²ι«ιι↙↙ι↑↖ι→  Try it online! Link is to verbose version of code. Explanation:  Ｎ Input number Ｅ Map over implicit range ² Literal 2 ι Current index Ｘ Exponentiate Ｆ « Loop over resulting powers of 2 ι Print right ι Print right again ↙ Move down and left ↙ι Print down and left ↑ Move up ↖ι Print up and left → Move right  • Now you could add another answer, go ahead! (It's quite weird putting irrelevant answers all-in-one.) – user92069 Jun 21 '20 at 5:04 • @Memberfor3months I could, but I don't know what I'd do with my ES7 answer, seeing as Arnauld outgolfed it anyway. – Neil Jun 21 '20 at 9:42 Charcoal, 27 bytes ＮαＷα«Ｘ²ι↙ＡＸ²⁻ι¹β↙β↑↖β→Ａ⁻α¹α  Try it online! Just modified my submission from the hexagon challenge a bit to make a triangle. • 16 bytes: ＦＥＮＸ²ι«ιι↙↙ι↑↖ι→ (1-indexed). – Neil Dec 27 '17 at 21:19 • @Neil I think this solution is different enough to warrant its own answer Dec 28 '17 at 18:00 Mathematica, 105 bytes ""<>Table[Which[i<1,"-",i==j,"\\",IntegerQ@Log2[i+j-1]&&j>i,"/",1>0," "],{i,0,2^#},{j,2*2^#}]~Riffle~"\n"&  where \n is replaced with a newline. Explanation: Makes an array of characters (indexed from i = 0 to 2^n vertically, j = 1 to 2^(n+1) horizontally) according to these rules: • If i<1 (so i = 0), put a "-" (for the top row) • If i==j, put a "\" (escaped, for the left-hand side) • If Log2[i+j-1] is an integer (so i+j-1 is a power of 2) and j>i, put a "/" (for the other edges) • And put " " everywhere else. Then ""<>...~Riffle~"\n"& turns this array into a string. JavaScript (JavaScript Shell), 136 132 bytes t=2**readline();p=print;p(v='-'.repeat(t*2));for(y=0;y<t;y++)p(' '.repeat(y)+'\\'+v.replace(/-/g,(_,i)=>i&-~i?' ':'/').slice(y*2+1))  Try it: readline = () => prompt(); print = (...args) => output += (args.join(' ') + '\n'); output = ''; t=2**readline();p=print;p(v='-'.repeat(t*2));for(y=0;y<t;y++)p(' '.repeat(y)+'\\'+v.replace(/-/g,(_,i)=>i&-~i?' ':'/').slice(y*2+1)) document.write(<pre>${output}</pre>);

• readline = prompt; probably suffices.
– Neil
Apr 15 '17 at 13:41

C, 200 bytes

#define P printf(
i,j,k,l;f(n){for(i=0,k=pow(2,n);i++<k;)P"--");for(i=0;i<k;++i){l=P"\n%*c",i+1,92);for(j=0;j<n;++j)if(2*(int)pow(2,j)+1-i>l)l+=P"%*c",2*(int)pow(2,j)+1-i-l,47);P"%*c",2*k-i-l+1,47);}}


Try it online!

JavaScript (ES6), 1089593 92 bytes

let f =

n=>(g=x=>y>1<<n?'':x<2<<n?' /\\-'[y?~x+y?+!(k=x+y,x<y|k&-k-k):2:3]+g(x+1):
+g(!++y))(y=0)

console.log(f(0))
console.log(f(1))
console.log(f(2))
console.log(f(3))

05AB1E, 45 bytes

I made it! (It's a fun challenge, I don't get why they VTC'd it)

<oLð×ε¨'\«}IL<oð×ε¨'/«}JDgU.sR2ô€нøJ»X>'-×s‚»


Try it online!

Canvas, 22 bytes

╵ｒＨ２；＾／Ｌ ×；＋ｎ｝ｌ＼ｎＬ-×；∔


Try it here!

Explanation

'rH2;^/L ×;+n}l\nL-×;∔
'                      increment input integer n
r                     range 0..n
H          }         push an empty string and start a loop
2;^                 get 2^i
/                antidiagonal of that length
L               get width(don't pop)
×;_           add that many spaces to the left
n          overlap with prev iteration
l        get height(don't pop)
\       create a diagonal of that length
n      and overlap with the created sides
L     get width(don't pop)
-×;∔ add that many minuses to the top



PHP, 137 Bytes

for(;$i<1+2**$a=$argn;++$i)for($n=0;$n<$m=2**($a+1);$s[]=2**++$n)echo$n%$m?"":"\n",$i?$i-1!=$n?$i<=$n&in_array($i+$n,$s)?"/":" ":"\\":"-";


Online Version

Expanded

for(;$i<1+2**$a=$argn;++$i)
for($n=0;$n<$m=2**($a+1);$s[]=2**++$n) # fill array for sum compare after loop
echo$n%$m?"":"\n", # print line feed
$i # false for first line ?$i-1!=$n # row number - 1 equal column print \ ?$i<=$n&in_array($i+$n,$s) # if row number lte column and sum of both in array print /
?"/"
:" " # else print space
:"\\"
:"-"; # fill first line


A recursive way for 199 Bytes

function f($a){global$r;for(;$i<1+2**$a;$i++)for($n=0;$n<$m=2**($a+1);$n++)$r[$i][$n]=$i?$i-1!=$n?$i<=$n&&$i+$n==2**($a+1)?"/":" ":"\\":"-";$a?f($a-1):0;}f($argn);echo join("\n",array_map("join",\$r));
`