8
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ can there be trailing/leading newlines or trailing spaces? \$\endgroup\$ – dzaima Sep 30 '17 at 15:20
  • \$\begingroup\$ @dzaima Yes, i'll edit that in. \$\endgroup\$ – Ian H. Sep 30 '17 at 20:48
10
\$\begingroup\$

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.

\$\endgroup\$
7
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ b=int(input()) and then if n:b=10-b saves a byte. \$\endgroup\$ – Lynn Oct 5 '17 at 19:52
5
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ 116 bytes \$\endgroup\$ – ovs Sep 30 '17 at 20:58
2
\$\begingroup\$

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)

\$\endgroup\$
2
\$\begingroup\$

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!

\$\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.