0
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ @BetaDecay Aww, come on. same author, no big deal. \$\endgroup\$ – Matthew Roh Apr 15 '17 at 12:20
  • 3
    \$\begingroup\$ Sure, but where does it end? Embedded squares? Embedded Circles? Embedded dodecagons? One challenge IMO is enough \$\endgroup\$ – Beta Decay Apr 15 '17 at 12:23
  • 5
    \$\begingroup\$ @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. \$\endgroup\$ – Comrade SparklePony Apr 15 '17 at 12:26
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – HyperNeutrino Apr 15 '17 at 14:08
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Rɪᴋᴇʀ Apr 15 '17 at 14:14
2
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ 16 bytes: FENX²ι«ιι↙↙ι↑↖ι→ (1-indexed). \$\endgroup\$ – Neil Dec 27 '17 at 21:19
  • \$\begingroup\$ @Neil I think this solution is different enough to warrant its own answer \$\endgroup\$ – user41805 Dec 28 '17 at 18:00
4
\$\begingroup\$

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
|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

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

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ readline = prompt; probably suffices. \$\endgroup\$ – Neil Apr 15 '17 at 13:41
1
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

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

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

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));
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.