37
\$\begingroup\$

Your task is to output a representation of the legendary tree of life, Yggdrasil.

You must write a program whose output is exactly that :

                                       /\
                                      /**\
                                     /****\
                                    /******\
                                    /******\
                                   /********\
                                  /**********\
                                 /************\
                                /**************\
                                 /************\
                                /**************\
                               /****************\
                              /******************\
                             /********************\
                            /**********************\
                              /******************\
                             /********************\
                            /**********************\
                           /************************\
                          /**************************\
                         /****************************\
                        /******************************\
                           /************************\
                          /**************************\
                         /****************************\
                        /******************************\
                       /********************************\
                      /**********************************\
                     /************************************\
                    /**************************************\
                        /******************************\
                       /********************************\
                      /**********************************\
                     /************************************\
                    /**************************************\
                   /****************************************\
                  /******************************************\
                 /********************************************\
                /**********************************************\
                     /************************************\
                    /**************************************\
                   /****************************************\
                  /******************************************\
                 /********************************************\
                /**********************************************\
               /************************************************\
              /**************************************************\
             /****************************************************\
            /******************************************************\
                  /******************************************\
                 /********************************************\
                /**********************************************\
               /************************************************\
              /**************************************************\
             /****************************************************\
            /******************************************************\
           /********************************************************\
          /**********************************************************\
         /************************************************************\
        /**************************************************************\
               /************************************************\
              /**************************************************\
             /****************************************************\
            /******************************************************\
           /********************************************************\
          /**********************************************************\
         /************************************************************\
        /**************************************************************\
       /****************************************************************\
      /******************************************************************\
     /********************************************************************\
    /**********************************************************************\
            /******************************************************\
           /********************************************************\
          /**********************************************************\
         /************************************************************\
        /**************************************************************\
       /****************************************************************\
      /******************************************************************\
     /********************************************************************\
    /**********************************************************************\
   /************************************************************************\
  /**************************************************************************\
 /****************************************************************************\
/******************************************************************************\
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |--------|
                                   |________|

There is no trailing whitespace. The final newline may be omitted.

To make checking easier, here are the md5 sums of the expected output:

  • 374899e56bc854d04639c43120642e37 - No newline at end.
  • 03afb81d960b8e130fe2f9e0906f0482 - Newline at end

Standard loopholes apply. This is code-golf, so the smallest entry in bytes win!

\$\endgroup\$
  • 10
    \$\begingroup\$ Related: codegolf.stackexchange.com/q/15860/15599 . Nax, you should work in marketing. Every household is going to want a Yggdrasil this Christmas. I see you answered the Graham's number question too. You must like pointy things. Let's have some new names for "rocket" and "ice cream cone." \$\endgroup\$ – Level River St Aug 19 '14 at 14:18
  • 6
    \$\begingroup\$ Of course that's an Yggdrasil, and definitely not yet-another-christmas-tree :) \$\endgroup\$ – Nax Aug 19 '14 at 14:25
  • 3
    \$\begingroup\$ I'm just imagining what happens if the md5 sums in the OP are incorrect :P \$\endgroup\$ – Mooing Duck Aug 19 '14 at 22:40
  • 4
    \$\begingroup\$ @Nax: since the world tree has fruit, and angiosperms don't, and that is clearly a confiner of some description, unfortunately it looks like you took a picture of the wrong tree :P \$\endgroup\$ – naught101 Aug 21 '14 at 14:28
  • 2
    \$\begingroup\$ Are MD5 collisions acceptable? \$\endgroup\$ – hexafraction Aug 21 '14 at 20:25

36 Answers 36

1
\$\begingroup\$

C (gcc), 163 160 bytes

-3 bytes thanks to ceilingcat.

j,w,s[99];main(i){for(;++i<12;w-=i-1)for(j=i+2;j--;w++)printf("%*c%.*s\\\n",40-w,47,w*2,memset(s,42,99));for(;i--;)printf("%44s|\n",i?"|--------":"|________");}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Is limiting the number of printed string characters by using "%.*s" in a printf call standard behaviour? \$\endgroup\$ – Jonathan Frech Feb 20 '18 at 17:42
  • \$\begingroup\$ @JonathanFrech Yep, should be. "If the precision is specified, no more than that many bytes are written." - §7.21.6.1 of this document. (It regards fprintf, but the section on printf states that the format string works the same way as for fprintf.) \$\endgroup\$ – gastropner Feb 21 '18 at 0:51
  • \$\begingroup\$ Suggest )printf("%*c%.*s\\\n",40-w++ instead of w++)printf("%*c%.*s\\\n",40-w \$\endgroup\$ – ceilingcat Dec 20 '18 at 3:09
1
\$\begingroup\$

Japt -R, 51 50 49 48 bytes

Ao*3 cÈôY+3@'\i'/+Xç*²
cCÆ'|i|+8ç"-_"gXzB})û mx1

Test it

Ao*3 cÈôY+3@'\i'/+Xç*²
A                             :10
 o                            :Range [0,A)
  *3                          :Multuply each by 3
     c                        :Flatten after ...
      È                       :  Map each X at 0-based index Y
       ôY+3                   :    Sorted range [X,X+Y+3]
           @                  :    Map each X
            '\i               :      Prepend "\" with
               '/+            :        "/" appended with
                  Xç          :          X times repeat
                    *²        :            "*" repeated twice
cCÆ'|i|+8ç"-_"gXzB})û mx1
c                             :Concatenate
 C                            :  12
  Æ                           :  Map each X in the range [0,C)
   '|i                        :    Prepend "|" with
      |+                      :      "|" appended with
        8ç                    :        8 times repeat
          "-_"g               :          Get the character in "-_" at index
               Xz             :            X floor divided by
                 B            :              11
                  }           :  End map
                   )          :End concat
                    û         :Centre pad each element with spaces to the length of the longest
                      m       :Map
                       x1     :  Trim right
                              :Implicit output, joined with newlines
\$\endgroup\$
1
\$\begingroup\$

Assembly (MIPS, SPIM), 768 671 659 655 654 bytes

.text
.globl main
main:
li $8 32
li $9 47
li $t2 42
li $t3 92
li $t4 10
li $t5 42
addi $sp -4
move $4 $sp
la $s6 q
la $s5 w
li $t8 0
li $s0 10
li $t9 0
li $s3 40
li $s2 39
q:
sub $s3 $s3 $t8
add $s2 $s2 $t8
addi $t7 $t8 3
addi $t8 1
blt $s0 $t8 e
r:
sw $0 ($4)
blt $t9 $s2 t
beq $t9 $s2 y
beq $t9 $s3 u
beqz $t7 i
sb $t2 ($4)
p:
li $2 4
syscall
addi $t9 1
ble $t9 $s3 r
move $t9 $0
j $s5
o:
addi $t7 -1
bgez $t7 r
jr $s6
w:
addi $s2 -1
addi $s3 1
j o
t:
sb $8 ($4)
j p
y:
sb $9 ($4)
j p
u:
sb $t4 1($4)
sb $t3 ($4)
j p
i:
sb $t5 ($4)
j p
e:
li $t1 124
li $t3 124
li $t2 45
li $t5 95
li $t7 11
la $s6 a
la $s5 o
li $s2 35
li $s3 44
j r
a:
li $2 10
syscall

Try it online!

Edit: Saved ~100 bytes by hard coding each character in a register and simply writing to the stack, then abusing the fact that MIPS doesn't have types so implicit word to ascii string is allowed.
Edit 2: removed duplicate stack alloc. (addi $sp -4) Oops!
Edit 3: in rewrite, accidently made the tree stump 1 character too short. (36 in stead of 35). Fixed.
Edit 4: -4 bytes by using $8 and $9 instead of $t0 and $t1. they mean the same thing, but $t0 is easier to read. unfortunately, all other t registers are the same length when written nicely ($t2 == $10, but both are 3 characters), so the rest are a wash. I could (in theory) use the remaining 'a' registers (5-7. print syscall uses 4), but afaik the behavior isn't defined when using syscall.
-1 Byte by setting t8 to 0 and branching on less than instead of on equal. also required me to reorder the add ops, which unfortunately made the execution time take a whopping 3 opcodes longer.

Not the best at MIPS, but after clearing my conscience of this in meta discussion, i'm submitting them. MIPS is not designed for this (no output, no for loops) but it's pretty fun to do this perhaps in spite of that.

Unfortunately, I haven't created a commented version for this new solution, but you can view the old version, with full documentation here

Feel free to take the credit and improve upon this answer if you can beat it - I'm probably missing a few optimizations here and there.

\$\endgroup\$
  • \$\begingroup\$ since this solution doesn't require .data, it's possible to convert to binary (compiled) and save a bunch of bytes. however, SPIM doesn't support it, so c'est la vie. \$\endgroup\$ – Andrew Baumher Sep 4 at 0:29
  • \$\begingroup\$ Also, I'm not sure if the shorthand addi $sp -4 which is really addi $sp $sp -4 is from SPIM or MIPS in general, so if anyone else does, please let me know. \$\endgroup\$ – Andrew Baumher Sep 4 at 1:16
0
\$\begingroup\$

Jelly, 56 bytes

4Ḷ4ịr+L}¥¥Ɗ9СẎḤ”*ẋ⁾/\jЀ;⁾-_x11,1¤ẋ8⁾||jƊ€¤µLH40_⁶ẋ;µ€Y

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 140 138 136 bytes

while($n--||30>$w+=3-$n=3+$i++)echo($p=str_pad)("",39-$w),$p("/",1|2*$w++,"*"),"\\
";while(-~$i)echo$p("",35),$p("|",9,$i--?"-":_),"|
";

less golfed:

for(;$i<10;$w-=$i)for($n=3+$i++;$n--;)
    echo str_pad("",39-$w), str_pad("/",1+2*$w++,"*"),"\\\n";
for($i++;$i--;)
    echo str_pad("",35), str_pad("|",9,$i?"-":_),"|\n";

Run with php -nr '<code>' or try it online. Requires PHP 7.

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 36 bytes

TLεÌÝN3*+'*×'/ì]˜…|--11и…|__ªº€¨«º.c

Try it online!

TLε            ]       # for y in [1 .. 10]
   ÌÝ                  #  [0 .. y+2]
     N                 #  0-based iteration count (effectively y-1)
      3*               #  times 3
        +              #  add (vectorizes over the range, yielding [3*y-3 .. 4*y-1])
         '*×           #  "*" repeated that many times (vectorizes)
            '/ì        #  prepend "/" (vectorizes)
˜                      # flatten
 …|--                  # literal "|--"
     11и               # repeated 11 times as an array
        …|__           # literal "|__"
            ª          # appended to that array
             º         # mirror (vectorizes: ["|----|", "|----|", ..., "|____|")
              ۬       # remove the last character of each
                «      # merge the two lists (["/", "/*", ..., "|----", ...])
                 º     # mirror
                  .c   # center
\$\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.