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\$ – ζ-- Aug 21 '14 at 20:25

36 Answers 36

17
\$\begingroup\$

CJam, 62 bytes

A,{I4+,{I3*J+_39\-S*'/@2*'**'\N}fJ}fI{35S*'|'-8*'|N++}C*'-/'_*

Try it online!

How it works

A,           " Push [ 0  …  9 ].                                                   ";
{            " For each I in that array:                                           ";
  I4+,       " Push [ 0  …  I + 3 ].                                               ";
  {          " For each J in that array:                                           ";
    I3*J+_   " Push K := 3 * I + J twice.                                          ";
    39\-S*   " Push a string consisting of 39 - K spaces.                          ";
    '/       " Push a slash.                                                       ";
    @2*'**   " Push a string consisting of 2 * K asterisks.                        ";
    '\N      " Push a backslash and a linefeed.                                    ";
  }fJ        "                                                                     ";
}fI          "                                                                     ";
{            " Do the following 12 times:                                          ";
  35S*       " Push a string consisting of 35 spaces.                              ";
  '|         " Push a vertical bar.                                                ";
  '-8*'|++   " Push the string '--------|\n'.                                      ";
}C*          "                                                                     ";
'-/'_*       " Replace the hyphen-minus signs of the last string with underscores. ";
\$\endgroup\$
  • 2
    \$\begingroup\$ I've got to start using CJam instead of GolfScript... \$\endgroup\$ – Claudiu Aug 20 '14 at 16:10
  • \$\begingroup\$ What makes CJam code so compact? Is it just usual features match together well or there is some super-trick? Or it's actually not CJam, but CJam programmers so? \$\endgroup\$ – Vi. Aug 21 '14 at 1:40
  • 2
    \$\begingroup\$ @Vi.: It's a mix of both. 1. CJam is stack based, so you usually get away without using any variables. 2. All built-ins' names are at most two characters long. Because of this, you don't need whitespace except for some special cases. 3. Even submissions in golfing languages don't golf themselves. My GolfScript/CJam programs are a lot shorter now than they used to be. \$\endgroup\$ – Dennis Aug 21 '14 at 3:29
  • \$\begingroup\$ Suggestion for CJam would be to use ASCII codes 128-256 for a whole nother set of built-ins. have some special editor that \$\endgroup\$ – Claudiu Aug 21 '14 at 19:36
  • \$\begingroup\$ @Claudiu: Well, that's up to its creator. If I designed my own golfing language, I think I would make it bit-based or encode all instructions arithmetically. \$\endgroup\$ – Dennis Aug 21 '14 at 23:30
23
\$\begingroup\$

Golfscript, 84 77 76 75 72 characters

Different approach from Howard's. Click on the character count to try it.

10,{.)4*,\3*>{.39\-' '*'/'@2*'*'*'\
'}/}/12,{' '35*'|'@11<'_-'1/=8*1$n}/

Thanks to Howard for saving 3 characters!

Explanation:

This is more or less a straightforward port of my Python solution. Taking some expressive liberties (using named variables instead of keeping track of stack positions, and print is really keeping things on the stack, not printing):

10,{...}/            # for i in range(10):
   .                 #    x = i
   )4*,              #    Y = range((i+1)*4)
   \3*               #    x *= 3
   >                 #    Y = Y[x:]  # y is now range(3*i, (i+1)*4)
   {...}/            #    for j in Y:
      .39\-          #        q = 39 - j
      ' '*           #        print ' '*q  # print right number of spaces
      '/'            #        print '/'
      @2*            #        j *= 2
      '*'*           #        print '*'*j
      '\<NEWLINE>'   #        print "\\\n"
12,{...}/            # for i in range(12):
    ' '35*           #    print ' '*35
    '|'              #    print '|'
    @11<             #    i = i < 11
    '_-'1/=          #    z = "_-"[i] # pick "-" if i < 11, else pick "_"
    8*               #    print z*8
    1$               #    print '|' # (copy from earlier in the stack)
    n                #    print "\n"
\$\endgroup\$
  • 1
    \$\begingroup\$ You may use the same line-break trick as in my solution. Instead of '\\'n pull the newline into the string (+/- 0 characters) and now the backslash no longer needs to be escaped (-1 character). \$\endgroup\$ – Howard Aug 20 '14 at 6:36
  • 1
    \$\begingroup\$ Two more: 4*4+ is the same as )4* and '_-'[=] can be written '_-'1/= which makes the last + obsolete. \$\endgroup\$ – Howard Aug 20 '14 at 6:50
  • 3
    \$\begingroup\$ an explanation could be useful...? is this too much to ask? \$\endgroup\$ – user16402 Aug 20 '14 at 8:29
  • \$\begingroup\$ @professorfish: Coming right up! \$\endgroup\$ – Claudiu Aug 20 '14 at 15:46
  • \$\begingroup\$ Don't you need a trailing ';' here? Otherwise there will be two trailing newlines since golfscript by default adds one. \$\endgroup\$ – Nabb Aug 24 '14 at 3:48
22
\$\begingroup\$

Haml & Sass

37 + 277 = 314

Haml:

%link(rel="stylesheet" href="s")
%pre

Sass:

pre:after{$l:"";@for$k from0 to10{@for$i from0 to4+$k{@for$j from0 to40-($k*6+$i*2)/2{$l:$l+' ';}$l:$l+'/';@for$j from0 to$k*6+$i*2{$l:$l+'*';}$l:$l+"\\\a ";}}@for$i from0 to12{@for$j from0 to35{$l:$l+" ";}@if$i<11{$l:$l+"|--------|\a ";}@else{$l:$l+"|________|";}}content:$l;}

*Sass allows control directives, concatenation, and variable dereference. All of which are useful when styling, but verbose while golfing.


gets generated into:

HTML:

<link rel="stylesheet" href="s" /><pre></pre>

CSS:

pre:after {
  content: "                                        /\\\a                                        /**\\\a                                       /****\\\a                                      /******\\\a                                      /******\\\a                                     /********\\\a                                    /**********\\\a                                   /************\\\a                                  /**************\\\a                                   /************\\\a                                  /**************\\\a                                 /****************\\\a                                /******************\\\a                               /********************\\\a                              /**********************\\\a                                /******************\\\a                               /********************\\\a                              /**********************\\\a                             /************************\\\a               
             /**************************\\\a                           /****************************\\\a                          /******************************\\\a                             /************************\\\a                            /**************************\\\a                           /****************************\\\a                          /******************************\\\a                         /********************************\\\a                        /**********************************\\\a                       /************************************\\\a                      /**************************************\\\a                          /******************************\\\a                         /********************************\\\a                        /**********************************\\\a                       /************************************\\\a                      /**************************************\\\a                     /******************************
**********\\\a                    /******************************************\\\a                   /********************************************\\\a                  /**********************************************\\\a                       /************************************\\\a                      /**************************************\\\a                     /****************************************\\\a                    /******************************************\\\a                   /********************************************\\\a                  /**********************************************\\\a                 /************************************************\\\a                /**************************************************\\\a               /****************************************************\\\a              /******************************************************\\\a                    /******************************************\\\a                   /**********************************
**********\\\a                  /**********************************************\\\a                 /************************************************\\\a                /**************************************************\\\a               /****************************************************\\\a              /******************************************************\\\a             /********************************************************\\\a            /**********************************************************\\\a           /************************************************************\\\a          /**************************************************************\\\a                 /************************************************\\\a                /**************************************************\\\a               /****************************************************\\\a              /******************************************************\\\a             /*****************************************************
***\\\a            /**********************************************************\\\a           /************************************************************\\\a          /**************************************************************\\\a         /****************************************************************\\\a        /******************************************************************\\\a       /********************************************************************\\\a      /**********************************************************************\\\a              /******************************************************\\\a             /********************************************************\\\a            /**********************************************************\\\a           /************************************************************\\\a          /**************************************************************\\\a         /****************************************************************\\\a        /*********
*********************************************************\\\a       /********************************************************************\\\a      /**********************************************************************\\\a     /************************************************************************\\\a    /**************************************************************************\\\a   /****************************************************************************\\\a  /******************************************************************************\\\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    |--------|\a                                    
|--------|\a                                    |--------|\a                                    |________|"; }

the resulting page

\$\endgroup\$
  • 1
    \$\begingroup\$ You don't see a ton of HTML/CSS solutions on CodeGolf. Nice job. \$\endgroup\$ – Mordred Aug 20 '14 at 20:02
16
\$\begingroup\$

GolfScript, 79 characters

10,{:^4+,{2*^6*+'*'*.,2/~40+' '*'/'@'\
'}/}/[' '35*]12*'|--------|
'*'||''_'8**

Try the code here. Note that the line breaks aren't optional in this code

\$\endgroup\$
  • 24
    \$\begingroup\$ A multi-line GolfScript solution? I didn't think I'd see the day! \$\endgroup\$ – Ingo Bürk Aug 19 '14 at 17:24
13
\$\begingroup\$

Python, 148 129 126 121 characters

R=range
for i in R(10):
 for j in R(i*3,4+i*4):print' '*(39-j)+'/'+'**'*j+'\\'
for c in'-'*11+'_':print' '*35+'|'+c*8+'|'

Thanks to Falko for saving 3 characters and to flornquake for brilliantly saving another 5!

\$\endgroup\$
  • 1
    \$\begingroup\$ Python's character multiplication is a bit too perfect for this challenge... \$\endgroup\$ – Sean Latham Aug 19 '14 at 15:24
  • \$\begingroup\$ Wait 'till you see my golfscript solution... =P \$\endgroup\$ – Claudiu Aug 19 '14 at 15:25
  • \$\begingroup\$ Character multiplication is perfect for any kolmogorov-complexity problem, but that's part of the game! \$\endgroup\$ – Nax Aug 19 '14 at 15:27
  • 2
    \$\begingroup\$ Use '**'*j to save 3 characters. Apart from this my solution converges to the exact same code. :) \$\endgroup\$ – Falko Aug 19 '14 at 22:37
  • 1
    \$\begingroup\$ You can make the last line shorter by writing for c in'-'*11+'_':print' '*35+'|'+c*8+'|'. \$\endgroup\$ – flornquake Aug 21 '14 at 8:54
11
\$\begingroup\$

Bash, 236 197 193

10 bytes of code + 1-byte filename + 182-byte data file = 193 bytes total

zcat y 2>j

Explanation

y is a file containing the Yggdrasil, without a trailing new line, compressed with the zopfli algorithm (invoked as zopfli --i64) and then with the last 8 bytes removed. zopfli is compatible with gzip, so I can decompress the file with standard Bash utilities designed for gzip. The size of the data file is 182 bytes (229 bytes when normal gzip is used). The size of the original Yggdrasil, without the trailing new line, is 5876 bytes.

The removal of the last 8 bytes causes error messages, which are suppressed by sending standard error to a file called j. If j exists, it will be overwritten.

The base64 of y is (use base64 -d to obtain the original file):

H4sIAAAAAAACA+3SMQoCURDA0N5TWC+IFxK8yD/8Nul9hfDRnamT6J+du83zdUPwOACNNDb0+3Bs
tMPhSscDvusHgM9wIcUNclL+5r/luJXkmlh5rM3r8txkMdVNcEn1Nc2a1AU72XWz3Xd91r5z7eZD
AQKQgAI0PDFf8xJfExpQgQhkpAIdz8ytzK3AQMg6UMIQpLQELU/NQc5B/thBegtqHpOc16jHOX/v
x1mPZg19MfrdrBM=

And the md5sum is:

4a049a80241160cdde0a3cbca323b7f2
\$\endgroup\$
  • 2
    \$\begingroup\$ My finger was itching to hit the "delete" button when I saw the code, until I read the character count and explanation. :P \$\endgroup\$ – Doorknob Aug 19 '14 at 19:13
  • 2
    \$\begingroup\$ You can save about 30 bytes by using zopfli to make the compressed file. "zopfli --i100 y" yields a 190-byte file that zcat can decompress. \$\endgroup\$ – Glenn Randers-Pehrson Aug 19 '14 at 22:44
  • 1
    \$\begingroup\$ And a few more if you delete the last 10 bytes and close STDERR. \$\endgroup\$ – Dennis Aug 19 '14 at 23:57
  • \$\begingroup\$ @GlennRanders-Pehrson thanks for pointing this out, I've also edited my answer to 'Molar masses of compounds' codegolf.stackexchange.com/a/35613/16402 to use zopfli, saving 37 bytes \$\endgroup\$ – user16402 Aug 20 '14 at 8:26
  • \$\begingroup\$ @Dennis it turned out to be 8 bytes (any more and bits of the Yggdrasil's trunk would be missing), thanks \$\endgroup\$ – user16402 Aug 20 '14 at 8:27
8
\$\begingroup\$

C, 169

i;j;p(a,b){while(b--)putchar(a);}main(){for(;i++<10;)for(j=i*3-4;++j<i*4;p(32,39-j),p(47,1),p(42,j*2),puts("\\"));for(++i;i--;p(32,35),p(124,1),p(i?45:95,8),puts("|"));}

Ungolfed (and slightly disentangled):

int i;
int j;
void p(a,b) {
  while (b--) putchar(a);
}
void main() {
  for (;i++<10;) {
    for (j=i*3-4;++j<i*4;) {
      p(32,39-j);
      p(47,1);
      p(42,j*2);
      puts("\\");
    }
  }
  for (++i;i--;) {
    p(32,35);
    p(124,1);
    p(i?45:95,8);
    puts("|");
  }
}
\$\endgroup\$
8
\$\begingroup\$

Ruby - 100

puts (0..21).map{|i|i>9??\ *35+?|+(i>20??_:?-)*8+?|:(0..i+3).map{|y|?\ *(39-z=y+3*i)+?/+?**z*2+?\\}}

Puts auto-flattens, so we can collect all the lines even in nested arrays. Needs Ruby 1.9

Try at ideone

\$\endgroup\$
  • \$\begingroup\$ I think it needs some leading spaces. Also, works with Ruby 1.9, but not in 1.8. \$\endgroup\$ – Scott Leadley Aug 19 '14 at 18:28
7
\$\begingroup\$

PowerShell 104 101

0..9|%{(3*$_)..(3+$_*4)|%{" "*(39-$_)+"/"+"*"*2*$_+"\"}};0..11|%{" "*35+"|"+("-","_")[$_-eq11]*8+"|"}
\$\endgroup\$
7
\$\begingroup\$

J, 98 88 84 75

(85 11 1#3 4$' /*\ |-| |_|')#"1~(39&-,1,+:,1:)"0(12$4),~85(-4&+#-:*>:)&i.10
\$\endgroup\$
6
\$\begingroup\$

C# 258 234bytes

Thanks to some annoymous user for the suggested edits making good use of the String.PadLeft method!

using System;class G{static void Main(){Action<string>p=Console.WriteLine;int i=0,j;for(;i++<10;)for(j=i*3-3;j++<i*4;)p("/".PadLeft(41-j)+"\\".PadLeft(2*j-1,'*'));while(i-->0)p("|--------|".PadLeft(45));p("|________|".PadLeft(45));}}

The code is pretty simple, not much left to golf.

Formatted code:

using System;
class G
{
    static void Main()
    {
        Action<string> p = Console.WriteLine;
        int i = 0, j;
        for(; i++ < 10 ;)
            for(j = i*3 - 3; j++ < i*4;)
                p("/".PadLeft(41 - j) + "\\".PadLeft(2*j - 1,'*'));
        while(i-- > 0)
            p("|--------|".PadLeft(45));
        p("|________|".PadLeft(45));
    }
}
\$\endgroup\$
  • \$\begingroup\$ Using \r is pointless here. This is code golf. \$\endgroup\$ – nyuszika7h Aug 19 '14 at 15:56
  • 3
    \$\begingroup\$ @nyuszika7h indeed, but it's included in the formatted copy for good measure so that people using notepad can more easily see the glory of the Yggdrasil (it's not included in the score) \$\endgroup\$ – VisualMelon Aug 19 '14 at 16:06
  • \$\begingroup\$ Oh, I see. Windows... Feel free to fix my edit then. \$\endgroup\$ – nyuszika7h Aug 19 '14 at 16:07
  • \$\begingroup\$ @nyuszika7h thanks for pointing out the Syntax Highlighting wasn't right \$\endgroup\$ – VisualMelon Aug 19 '14 at 16:10
5
\$\begingroup\$

Perl, 127

for$i(0..9){for$j($i*3..3+$i*4){print" "x(39-$j),"/","*"x($j*2),"\\\n";}}for$i(0..11){print" "x35,"|",($i>10?"_":"-")x8,"|\n";}

Ungolfed:

for $i (0..9) {
  for $j ($i*3..3+$i*4) {
    print " "x(39-$j) , "/" , "*"x($j*2) , "\\\n";
  }
}
for $i (0..11) {
  print " "x35 , "|" , ($i>10?"_":"-")x8 , "|\n";
}
\$\endgroup\$
  • \$\begingroup\$ Replace $i with $_ and save 4 characters. \$\endgroup\$ – choroba Aug 24 '14 at 17:09
5
\$\begingroup\$

Ruby - 139 129 126 123 121

Hoisted "puts" outside of array creation (suggestion from bitpwner).

puts (0..9).map{|i|(i*3...(i+1)*4).map{|j|"/#{'**'*j}\\".rjust(41+j,' ')}}+["%45s"%'|--------|']*11<<'%45s'%'|________|'


Ungolfed ("puts" unhoisted):


# print fill patterns from the intervals
# [0..3, 3..7, 6..11, 9..15, 12..19, 15..23, 18..27, 21..31, 24..35, 27..39]
# centered on columns 81-82
(0..9).each { |i|
  (i*3...(i+1)*4).each { |j|    # x...y vs x..y-1 saves a char
    puts "/#{'**'*j}\\".rjust(41+j,' ')
  }
}
# print the stump
puts ("%45s\n" % '|--------|') * 11
puts '%45s' % '|________|'
\$\endgroup\$
  • \$\begingroup\$ @bitpwner I think this deserves it's own answer. Go for it. \$\endgroup\$ – Scott Leadley Aug 19 '14 at 16:58
5
\$\begingroup\$

PHP 223 202 181 160 156

Edit I figured out how to alias a function with a variable and was able to chop off some more characters. That str_repeat function was really verbose

Edit 2: Thanks everyone for the suggestions!

Golfed:

<?$s=str_repeat;for($i=-1;$i++<9;)for($j=$i*3;$j<4+$i*4;)echo$s(' ',39-$j).'/'.$s('**',$j++)."\\
";for($i=12;$i--;)echo$s(' ',35),'|'.$s($i?'-':'_',8)."|
";

Readable:

    <?

$s=str_repeat;

for($i=-1;$i++<9;)
{
    for($j=$i*3;$j<4+$i*4;)
    {
        echo$s(' ',39-$j).'/'.$s('**',$j++)."\\
        ";
    }
}

for($i=12;$i--;)
{
    echo$s(' ',35),'|'.$s($i?'-':'_',8)."|
    ";
}

Output: http://brobin.me/yggdrasil.php

\$\endgroup\$
  • \$\begingroup\$ You can save 6 chars by removing parenthesis around (39-$j), ($j*2) and ($i==11). -2 by replacing \n with real new line characters (PHP allows it in litterals). -5 chars by replacing (both) for($i=0;$i<12;$i++) with for($i=12;$i-->0;) and $i==11 with $i==0. \$\endgroup\$ – sebcap26 Aug 21 '14 at 10:56
  • \$\begingroup\$ After that you can also replace $s(($i==0?'_':'-'),8) with $s($i?'-':'_',8). (==> -5 chars) \$\endgroup\$ – sebcap26 Aug 21 '14 at 11:04
  • \$\begingroup\$ -1 Character by replacing $s('*',$j*2) with $s('**',$j). I think that's the last improvement that I can find. \$\endgroup\$ – sebcap26 Aug 21 '14 at 11:44
  • \$\begingroup\$ -2 changing ($j*2) in inner for to ($j++*2) and removing $j++ from for() arguments \$\endgroup\$ – avall Aug 21 '14 at 12:47
  • \$\begingroup\$ @sebcap26 for($i=12;$i-->0;) can be shortened to for($i=12;$i--;) - another 2 chars. \$\endgroup\$ – avall Aug 21 '14 at 13:23
4
\$\begingroup\$

JavaScript (console.log), 168 166

(Whitespace for readability only)

for(i=c=0;i<11;i++)
  for(j=0;j<4+i&!c;j++)
    l=i*13+j,
    a=Array(n=i-10?3*i+j+1:5).join("*-_"[k=(l>129)+(c=l==141)]),
    console.log(Array(41-n).join(" ")+'/||'[k]+a+a+'\\||'[k])
\$\endgroup\$
4
\$\begingroup\$

C (219)

Thanks to everyone for the golfing tips -- managed to get it down to 219. Don't think it'll go much lower.

w,W,s,S,i,r;main(){char T[78];memset(T,42,78);for(r=4,s=39;r<14;++r,s-=3,w+=6)for(i=0,S=s,W=w;i<r;++i,W+=2,--S)printf("%*s/%.*s\\\n",S,"",W,T);for(i=0;i<11;i++)printf("%35s|--------|\n","");printf("%35s|________|","");}

Required includes:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
\$\endgroup\$
  • \$\begingroup\$ You should start by removing newlines, they aren't compulsory in C, except for preprocessor directives. Also, you can abuse of C89 implicit int rule, golfing a little more. Nice anyway :) \$\endgroup\$ – Nax Aug 19 '14 at 15:20
  • \$\begingroup\$ If he's willing to use C89 he can omit all the includes as well. There is no case in this code where implicit declaration on call screws up. \$\endgroup\$ – Joshua Aug 19 '14 at 15:24
  • \$\begingroup\$ @Nax; that gets me to 263 -- and I pulled includes out. Thanks. \$\endgroup\$ – DreamWarrior Aug 19 '14 at 15:24
  • \$\begingroup\$ @Joshua -- 64 bit systems and memset could be problematic, but...otherwise, maybe not. Either way, most people remove the includes, so I did, too. \$\endgroup\$ – DreamWarrior Aug 19 '14 at 15:26
  • \$\begingroup\$ It's actually 262 characters, you don't need the trailing newline. \$\endgroup\$ – nyuszika7h Aug 19 '14 at 15:49
4
\$\begingroup\$

Haskell, 153 148

Straight-forward, no tricks, just plain golfing:

(a:b:c)%n=(39-n)&' '++a:(2*n)&b++c
n#m=[n..m]++(n+3)#(m+4)
(&)=replicate
main=putStr$unlines$map("/*\\"%)(take 85$0#3)++map("|-|"%)(11&4)++["|_|"%4]

The % operator draws a single line, its first argument being a String of length 3 containing the borders and the fill characters in that line (now assuming exactly 3 Chars, saving 5 bytes), the second, an Int, specifies half the number of fill characters. Pattern matching, cons-ing and appending is used in combination in order to save bytes by taking care of the "glue" between Char and String. In this second version, I also made # infinite and introduced a take 85 to make it finite again (no bytes saved, unfortunately).

The # operator creates the sequence for the n argument to % required for the tree: [0..3], [3..7], ... concatenated.

& is just an infix shorthand for replicate, which occurs three times.

The tree is put together in the last line, the newlines are added by unlines.

\$\endgroup\$
3
\$\begingroup\$

Delphi 429

Will try to improve later.
Golfed

uses strutils,SysUtils,Classes;const a='|----';b='|____';c:array [0..9,0..1]of int32=((0,3),(3,7),(6,11),(9,15),(12,19),(15,23),(18,27),(21,31),(24,35),(27,39));var t:TStrings;i,j:integer;begin t:=tstringlist.Create;for I:=0to 9do for J:=c[i,0]to c[i,1]do t.Add('/'+StringOfChar('*',j));for I:=0to 10do t.Add(a);t.Add(b);for I:=0to t.Count-1do t[i]:=t[i].PadLeft(40)+ReverseString(t[i]).Replace('/','\');write(T.TEXT);readln;end.

ungolfed

uses
  strutils,SysUtils,Classes;
const
  a='|----';
  b='|____';
  c:array [0..9,0..1]of int32=((0,3),(3,7),(6,11),(9,15),(12,19),(15,23),(18,27),(21,31),(24,35),(27,39));
var
  t:TStrings;
  i,j:integer;
begin
  t:=tstringlist.Create;
  for I:=0to 9do
    for J:=c[i,0]to c[i,1]do
      t.Add('/'+StringOfChar('*',j));
  for I:=0to 10do
    t.Add(a);
  t.Add(b);
  for I:=0to t.Count-1do
    t[i]:=t[i].PadLeft(40)+ReverseString(t[i]).Replace('/','\');
  write(T.TEXT);
  readln;
end.
\$\endgroup\$
3
\$\begingroup\$

Javascript, 288 281

Chrome hides duplicated console.logs, use IE instead.

function t(w){s="";for(i=0;i++<39-w;)s+=" ";s+="/";for(i=0;i++<w*2;)s+="*";return s+"\\"}function r(l){s="";for(i=0;i++<36;)s+=" ";s+="|";for(i=0;i++<8;)s+=l;return s+"|"}w=0;c=console;for(h=0;h++<10;){for(j=0;j++<3+h;)c.log(t(w++));w-=h}for(j=0;j++<11;)c.log(r('-'));c.log(r('_'))

Ungolfed:

function t(w)
{
    s="";
    for(i=0;i++<39-w;)
        s+=" ";
    s+="/";
    for(i=0;i++<w*2;)
        s+="*";
    return s+"\\"
}
function r(l)
{
    s="";
    for(i=0;i++<36;)
        s+=" ";
    s+="|";
    for(i=0;i++<8;)
        s+=l;
    return s+"|"
}

w=0;
c=console;
for(h=0;h++<10;)
{
    for(j=0;j++<3+h;)
        c.log(t(w++));
    w-=h;
}
for(j=0;j++<11;)
    c.log(r('-'));
c.log(r('_'))
\$\endgroup\$
  • \$\begingroup\$ Use for(i=0;i++<39-w;) instead of for(i=0;i<39-w;++i), and so on. Also, p=console.log instead of c=console and p instead of c.log (although I only tested the latter in Node.js, it doesn't work in Firefox). This saves 12 bytes. \$\endgroup\$ – nyuszika7h Aug 19 '14 at 16:10
  • \$\begingroup\$ c=console.log didn't work in Chrome either, so I didn't use it. The for loop tricks save 7 bytes though. \$\endgroup\$ – Sean Latham Aug 19 '14 at 16:33
  • \$\begingroup\$ I'm usually quite wary of anything encouraging me to use MSIE... \$\endgroup\$ – user16402 Aug 19 '14 at 17:45
  • \$\begingroup\$ This is quite possibly the first time I've ever seen the phrase "use IE instead." O_o However, you can suppress Chrome's behavior by simply running this code in the console instead of from a webpage. \$\endgroup\$ – Doorknob Aug 19 '14 at 19:17
  • \$\begingroup\$ Running it in a page and in the console produces the same results for me on both Firefox and Chrome. \$\endgroup\$ – Sean Latham Aug 19 '14 at 20:05
3
\$\begingroup\$

Lua - 164

a=' 'for i=0,9 do for j=i*3,3+i*4 do print(a:rep(39-j)..'/'..(('*'):rep(j*2))..'\\')end end for i=0,11 do print(a:rep(35)..'|'..((i>10 and'_'or'-'):rep(8))..'|')end
\$\endgroup\$
3
\$\begingroup\$

Mathematica 191 178

For sure not the best solution:

n=Nest;
t=Table;
""<>
    t[
        {n[#<>" "&,"",39-i],"/",n[#<>"*"&,"",2i],"\\\n"}
    ,
        {i,Flatten@t[Range[j+4]-1+3j,{j,0,9}]}
    ]
<>
    t[
        n[#<>" "&,"",35]<>If[i==12,"|________|\n","|--------|\n"]
    ,
        {i,12}
    ]

Not counting newlines. Mathematica skews the output, as it doesn't take the same width for a whitespace as for "*" and "/". But the result is correct.

\$\endgroup\$
  • \$\begingroup\$ (it's my first golf) \$\endgroup\$ – freddieknets Aug 21 '14 at 3:09
3
\$\begingroup\$

Java - 286

My first golf.

Golfed:

class M{public static void main(String[]args){int i=0,a,f=0;String s="";for(;i++<11;){for(a=i*3-4;++a<i*4;){if(i>10){a=4;if(++f>12)break;}s+=s.format("%"+(40-a)+"s"+(a>0?"%0"+a+"d":"")+"%3$s",f>0?"|":"/",0,f>0?"|":"\\").replace("0",f<1?"**":f>11?"__":"--")+"\n";}}System.out.println(s);}}

Ungolfed:

class M {
    public static void main(String[] args) {
        int i=0,a,f=0;
        String s = "";
        for(;i++<11;){
            for(a=i*3-4;++a<i*4;a++){
                if(i>10){
                    a=4;
                    if(++f>12)break;
                }
                s+=s.format("%"+(40-a)+"s"+(a>0?"%0"+a+"d":"")+"%3$s",f>0?"|":"/", 0,f>0?"|":"\\").replace("0", f<1?"**":f>11?"__":"--")+"\n";
            }
        }
        System.out.println(s);
    }
}

Test here

\$\endgroup\$
3
\$\begingroup\$

Powershell, 88 bytes

0..9|%{(3*$_)..(3+$_*4)|%{' '*(39-$_)+"/$('**'*$_)\"}}
,'-'*10+'_'|%{' '*35+"|$($_*8)|"}
\$\endgroup\$
2
\$\begingroup\$

Groovy 118

10.times{(it*3).upto 3+it*4,{println' '*(39-it)+'/'+'*'*it*2+'\\'}};12.times{println' '*35+'|'+(it>10?'_':'-')*8+'|'}
\$\endgroup\$
  • 1
    \$\begingroup\$ Why am I not surprised to see a pretty short Groovy solution. My impression of Groovy was that it was done by a Java programmer who hated typing. \$\endgroup\$ – Almo Aug 21 '14 at 13:38
2
\$\begingroup\$

C,194

This code is a hot mess and can definitely be golfed more. Still, it was an opportunity to try out a couple of things I've never done before: using a literal for a string of asterisks, and using the width specifier * with a string in printf.

i,j=5,k=5,n;
main(){
  char d[]={[0 ...77]=42,0};
  for(;i<85;i++)k--,j+=!k,k+=(j-1)*!k,n=39-i+(j-5)*(j-4)/2,printf("%*s/%s\\\n",n,"",d+n*2);
  for(;i<97;i++)printf("%*s|\n",44,i-96?"|--------":"|________");
}
\$\endgroup\$
2
\$\begingroup\$

Python 2, 117

j=0
while j<40:j-=j/4;exec(j/3+4)*r"print' '*(39-j)+'/'+'**'*j+'\\';j+=1;"
for c in'-'*11+'_':print' '*35+'|'+c*8+'|'

Other versions I tried include:

# 118
for i in range(10):j=i*3;exec(i+4)*r"print' '*(39-j)+'/'+'**'*j+'\\';j+=1;"
for c in'-'*11+'_':print' '*35+'|'+c*8+'|'

# 118
i=j=4
while j:j=52-3*i;exec"j-=1;print' '*j+'/'+'**'*(39-j)+'\\\\';"*i;i+=1
for c in'-'*11+'_':print' '*35+'|'+c*8+'|'

# 118
j=0
for b in'\\'*10:j-=j/4;exec(j/3+4)*"print' '*(39-j)+'/'+'**'*j+b;j+=1;"
for c in'-'*11+'_':print' '*35+'|'+c*8+'|'

# 119
s=40
while s:s+=10-s/4;exec(52-s)/3*r"s-=1;print' '*s+'/'+'**'*(39-s)+'\\';"
for c in'-'*11+'_':print' '*35+'|'+c*8+'|'
\$\endgroup\$
2
\$\begingroup\$

Pyth, 101 100 99 90 81 bytes

J0Km/*+d7d2S9V85FTKI!-TN=hZ=+JZ))+*d-39-NJ++\/*"**"-NJ\\)Va*]\-11\_+++*d35\|*N8\|

Try it online!

Python 3 translation:
Z=0
d=" "
J=0
K=list(map(lambda d:((d+7)*d)//2,range(1,10)))
for N in range(85):
    for T in K:
        if not T-N:
            Z+=1
            J+=Z
    print(d*(39-(N-J))+"/"+"**"*(N-J)+"\\")
for N in ["-"]*11+["_"]:
    print(d*35+"|"+N*8+"|")
\$\endgroup\$
  • \$\begingroup\$ Isn't the a function defined in your Python equivalent to + on lists? \$\endgroup\$ – Esolanging Fruit Feb 20 '18 at 1:34
  • \$\begingroup\$ @EsolangingFruit It's close, but not exactly: <list>+<str> is not a valid operation, but I could just as easily use ["-"]*11+["_"]. I decided to define my own function to more accurately translate what Pyth is actually doing. \$\endgroup\$ – hakr14 Feb 20 '18 at 17:24
  • \$\begingroup\$ Okay, that makes sense. \$\endgroup\$ – Esolanging Fruit Feb 21 '18 at 4:29
1
\$\begingroup\$

Cobra - 166 228 260

class P
    def main
        for x in 10,for n in 4+x,print' '.repeat(39-x*3-n)+'/['**'.repeat(x*3+n)]\\'
        w=' '.repeat(35)
        print'[w]|--------|\n'.repeat(11)+w+'|________|'
\$\endgroup\$
1
\$\begingroup\$

Python (313)

'eJztmEsOwjAMBfecoutIFUfiIjk8FAJNE3+eE0sI6qxnRq3jVZYFOlcQSwnlMPIJeqOFRNgPCsAVq9MH+Lvxhtb4DlcEgpcNUvjBr2IMyWEVQRIc3hKlk/2VYnGeqjEi4NEmJMZwrCpoUi6sErLB7W2THKPGXe9RG+3WN+tNYMA/FoYCcX9/dH+DhboxnKgiE429MhWJ3egSsRttZrLy7kxnSsih8yq5hGL9+Eqsn1TyWz+n0tZySz1ijq2UXGPQi1pey8mXoM9F38rJd5RXZaM='.decode('base64').decode('zip')
\$\endgroup\$
  • \$\begingroup\$ Doesn't output anything. With print,the backslashes and linefeeds seem to be missing. \$\endgroup\$ – Dennis Aug 22 '14 at 17:21
1
\$\begingroup\$

Racket 223 220 211 204 198

Golfed:

(for-each display(flatten(let([m make-list])`(,(for*/list([i 10][j(range(* i 3)(* 4(+ 1 i)))])`(,(m(- 39 j)" ")"/",(m j"**")"\\\n")),(for/list([c`(,@(m 11"-")"_")])`(,(m 35" ")"|",(m 8 c)"|\n"))))))

Ungolfed:

(for-each display
     (flatten
      (let([m make-list])
        `(,(for*/list([i 10][j(range(* i 3)(* 4(+ 1 i)))])
             `(,(m(- 39 j)" ")"/",(m j"**")"\\\n"))
          ,(for/list([c`(,@(m 11"-")"_")])
             `(,(m 35" ")"|",(m 8 c)"|\n"))))))
\$\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.