Life and Death of Trees

8

1

The Challenge

Write a program that takes an integer N as input, and outputs the stage of life or death that a tree is currently in. N will always be between 0 and 10.

The Stages of Life and Death

  • Life: To draw a stage of life, simply draw a base of size N, consisting of ^ characters, then move up and substract 2 from N, if the result is greater than 0, and draw a line of ^ characters again, again with the size of the new N. Repeat while N is larger than 0. To finish off, place either a single | character if the startoff value of N is odd, or two || characters if it was even.
    Example: N = 5

      ^
     ^^^
    ^^^^^
      |
    

    Note that if N = 0, only the stem will be drawn, without the leaves (^) on them.

  • Death: This is roughly the same as Life, except that the base is 10 - N, you use M instead of ^ and you add 2 to N and redraw the lines with the same procedure as above, but this time you add 2 to N while it is smaller or equal to 10.
    Example: N = 3

       M
      MMM
     MMMMM
    MMMMMMM
       |
    

The Goal

Your program has to run in 2 different languages both have to take a input N. The first language has to output the life stage of a tree according to N, the second language has to output the death stage.

Rules

  • This is , the shortest code in bytes that meets all the requirements wins.
  • The code has to run in 2 different languages. Yes, Python 2 and Python 3 are different languages.
  • Standard loopholes are forbidden.
  • Trailing newlines or spaces are allowed.

Test Cases

Input: N = 1

First language (life):

 ^
 |

Second language (death):

    M
   MMM
  MMMMM
 MMMMMMM
MMMMMMMMM
    |

Input: N = 10

First language (life):

    ^^
   ^^^^
  ^^^^^^
 ^^^^^^^^
^^^^^^^^^^
    ||

Second language (death):

||

Good luck coding!

Ian H.

Posted 2017-09-30T14:41:36.753

Reputation: 2 431

can there be trailing/leading newlines or trailing spaces? – dzaima – 2017-09-30T15:20:31.357

@dzaima Yes, i'll edit that in. – Ian H. – 2017-09-30T20:48:53.187

Answers

10

Dyalog APL / SOGL, 70 65 63 62 bytes

0000000: 7b0d 04eb c0fd 5832 5c49 e32a 2e4c ac20  {.....X2\I.*.L. 
0000010: 4d2a 5b3a 836b 6b7d 01ea 500d 9b31 a90f  M*[:.kk}..P..1..
0000020: 7b0f c285 c8c7 b932 c00e f8b1 c70f 7db0  {......2......}.
0000030: 32c2 8531 9c85 80b2 b50f aa32 f97d       2..1.......2.}

Interpreted in the classic Dyalog APL encoding: - life: (contains unprintables), expects ⎕IO←1

{' ^|⎕X2I┤z.L?pMzÅÏÏ}ûP'[1+⍵{⍵,⍨⌽⍉(2|⍺)↓⍉⍵}↑2,⍨1/⍨¨⍳⌈⍵÷2]}

TryAPL here!

{' ^|..'[1+⍵{⍵,⍨⌽⍉(2|⍺)↓⍉⍵}↑2,⍨1/⍨¨⍳⌈⍵÷2]}

         1+      add 1 to every value
 ' ^|..'[  ....] index the array getten below into " ^|.." with SOGLs code never being indexed in


⍵{..}↑2,⍨1/⍨¨⍳⌈⍵÷2

          1/⍨¨      replace each with that many 1s
              ⍳     the first ... numbers
               ⌈      ceiling of
                ⍵÷2     the right argument divided by 2
       2,⍨          append 2
      ↑             mix; convert to a 2D array, padding with zeroes
⍵{..}               execute that function with the arguments ⍵ - my right argument and the result of above


   {⍵,⍨⌽⍉(2|⍺)↓⍉⍵}  helper function
    ⍵,⍨             append the right arg to
               ⍉⍵     the right arg transposed
              ↓       without the first ... rows
         (2|⍺)          remainder of left arg ÷ 2
       ⌽⍉             transposed and reversed

Interpreted in SOGLs encoding - death:

{∑⁴╝υ”X2\I┐*.Lκ M*[:≥kk}¹╚P∑Β1Ι»{»φ√ωΩΡ2υ«⌠ΝΩ»}μ2φ√1β√↓νΟ»ι2⌡}

Try it Here!

Explanation:

{∑⁴╝υ”X                         push "{∑⁴╝υ" and pop it - noop
       2\                       push input divides by 2
         I┐*                    get that many + 1 vertical bars
            .Lκ                 push 10-input
                M*              get that many "M"s as a string
                  [    }        while ToS isn't falsy (here: isn't "")
                   :              duplicate the string
                    ≥             put the duplicate at the stacks bottom
                     kk           remove the last 2 letters (does nothing when the string is empty)
                        ¹       wrap everything in an array
                         ╚      center horizontally
                          P     print that
                           ...  execute a bunch of random nonsense which luckily does nothing

This took way too long.. Golfing tips for the APL part are welcome, everything mostly should be applicable as long as it doesn't change anything before ûP'

You can load the file (after reversing xxd) in SOGL by selecting the file in the Browse... dialog and then clicking load SOGL codepage encoded file.

dzaima

Posted 2017-09-30T14:41:36.753

Reputation: 19 048

7

Python 2 and Python 3, 119 110 109 bytes

r=p='';n=1/2>0;b=(-1)**n*int(input())+10*n
while b>0:r=p+'^M'[n]*b+'\n'+r;b-=2;p+=' '
print(r+p[1:]+'||'[b:])

Try it online! (Python 2, life)

Try it online! (Python 3, death)

ovs

Posted 2017-09-30T14:41:36.753

Reputation: 21 408

b=int(input()) and then if n:b=10-b saves a byte. – Lynn – 2017-10-05T19:52:32.950

5

Python 2 / Python 3, 121 bytes

def f(n):
 m=-1;n,c=(n,10-n,'^','M')[1/2>0::2];s=''
 while n>0:s=' '*m+c*n+'\n'+s;m+=1;n-=2
 return s+' '*m+'|'*(n%2or 2)

Try it online! (Python 2)

Try it online! (Python 3)

Erik the Outgolfer

Posted 2017-09-30T14:41:36.753

Reputation: 38 134

116 bytes – ovs – 2017-09-30T20:58:47.813

2

Python 3/Python 2, 128 bytes

def f(n,a=1/2>0):
 m=[n,10-n][a];k=-~m%2+1
 for j in range(k,m+1,2):print(" "*((m-j)//2)+"^M"[a]*j)
 print(" "*((m-k)//2)+"|"*k)

Try it online! (Python 3)

Try it online! (Python 2)

fireflame241

Posted 2017-09-30T14:41:36.753

Reputation: 7 021

126 bytes – Mr. Xcoder – 2017-09-30T15:26:07.373

124 bytes – Mr. Xcoder – 2017-09-30T15:29:59.807

2

C (gcc) / Python 2, 308 bytes

#define _\
"""
main(N,n,j,k){scanf("%d",&N);j=-~!(N%2);for(n=j;n<=N;n+=2){printf("%*c",(N-n)/2+1,94);for(k=n-1;k;k--)printf("^");puts("");}printf("%*c",++N/2,124);N%2?puts("|"):0;}
#define _"""
#define/*
N=10-input();j=[1,0][N%2]
for n in range(1,-~N,2):print(N-n)/2*" "+"M"*(n+j)
print~-N/2*" "+"|"*-~j
#*/_

Try it online!

Python 2 / C (gcc), 308 bytes

#define _\
"""
main(N,n,j,k){scanf("%d",&N);j=-~!(N%2);for(n=j;n<=N;n+=2){printf("%*c",(N-n)/2+1,94);for(k=n-1;k;k--)printf("^");puts("");}printf("%*c",++N/2,124);N%2?puts("|"):0;}
#define _"""
#define/*
N=10-input();j=[1,0][N%2]
for n in range(1,-~N,2):print(N-n)/2*" "+"M"*(n+j)
print~-N/2*" "+"|"*-~j
#*/_

Try it online!

Jonathan Frech

Posted 2017-09-30T14:41:36.753

Reputation: 6 681