Embedded Triangles!

0

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.

Comrade SparklePony

Posted 2017-04-15T12:04:08.703

Reputation: 5 784

1@BetaDecay Aww, come on. same author, no big deal. – Matthew Roh – 2017-04-15T12:20:48.370

3Sure, but where does it end? Embedded squares? Embedded Circles? Embedded dodecagons? One challenge IMO is enough – Beta Decay – 2017-04-15T12:23:16.343

5@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. – Comrade SparklePony – 2017-04-15T12:26:23.777

1I'm nominating to reopen this question because I don't see how the hexagon challenge's answers could be trivially copied to this challenge. – HyperNeutrino – 2017-04-15T14:08:14.627

1I'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. – Rɪᴋᴇʀ – 2017-04-15T14:14:46.690

Answers

2

Charcoal, 27 bytes

NαWα«X²ι↙AX²⁻ι¹β↙β↑↖β→A⁻α¹α

Try it online!

Just modified my submission from the hexagon challenge a bit to make a triangle.

user41805

Posted 2017-04-15T12:04:08.703

Reputation: 16 320

16 bytes: FENX²ι«ιι↙↙ι↑↖ι→ (1-indexed). – Neil – 2017-12-27T21:19:18.630

@Neil I think this solution is different enough to warrant its own answer – user41805 – 2017-12-28T18:00:23.030

4

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:

FENX²ι«ιι↙↙ι↑↖ι→

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

  N                 Input number
 E                  Map over implicit range
    ²               Literal 2
     ι              Current index
   X                Exponentiate
F     «             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

Neil

Posted 2017-04-15T12:04:08.703

Reputation: 95 035

2

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.

Not a tree

Posted 2017-04-15T12:04:08.703

Reputation: 3 106

1

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>`);

tsh

Posted 2017-04-15T12:04:08.703

Reputation: 13 072

readline = prompt; probably suffices. – Neil – 2017-04-15T13:41:14.097

1

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!

Steadybox

Posted 2017-04-15T12:04:08.703

Reputation: 15 798

1

JavaScript (ES6), 108 95 93 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))

Arnauld

Posted 2017-04-15T12:04:08.703

Reputation: 111 334

0

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));

Jörg Hülsermann

Posted 2017-04-15T12:04:08.703

Reputation: 13 026