27
\$\begingroup\$

Introduction

Yesterday I saw a birthday puzzle. Congrats!!

Also this week I watched an episode of the TV show Bones where a dead body was found buried under a tree. To calculate the time of death, they counted the tree rings.

Tree rings form because trees grow slower during the winter and faster during the summer. Thus you can calculate the tree's age by counting the rings. Also you can see natural events like rainy or dry seasons.

enter image description here

Challenge

Given an integer n >= 1 as input, write a full program to output the tree age rings.

Because rings can change of shape use three diferent characters ('0', '*', '+') to show climate cycles.

Age 1

0

Age 2

***
*0*
***

Age 3

+++++
+***+
+*0*+
+***+
+++++

Age 4

0000000
0+++++0
0+***+0
0+*0*+0
0+***+0
0+++++0
0000000

Size of the tree is a square of sides 2*n - 1

Winning

Shortest code in bytes wins.

\$\endgroup\$
9
  • \$\begingroup\$ What about when age=5? \$\endgroup\$
    – Blue
    Commented Sep 9, 2015 at 17:14
  • 4
    \$\begingroup\$ the rings have a three steps cycle.('0', '*', '+') so 5 year is * \$\endgroup\$ Commented Sep 9, 2015 at 17:15
  • \$\begingroup\$ Is the size the area, the perimeter or the length of each side? \$\endgroup\$
    – Beta Decay
    Commented Sep 10, 2015 at 6:11
  • \$\begingroup\$ @BetaDecay I update the question. If you can suggest a better wording let me know. \$\endgroup\$ Commented Sep 10, 2015 at 15:08
  • \$\begingroup\$ I think this question would have some more character to it if you had to paint a tree's growth pattern incorporating the effects of the seasons and natural events. \$\endgroup\$
    – Luminous
    Commented Sep 10, 2015 at 17:14

24 Answers 24

12
\$\begingroup\$

BBC Basic, 93 bytes

1I.r:r=r-1:F.i=-r TOr:F.j=-r TOr:p=ABS(i):q=ABS(j):IFp<q TH.p=q
2V.48-(p MOD3)*6MOD7:N.:P.:N.

The abbreviated keywords help out a lot here. In line 2, I'm using the VDU command (equivalent to C's putchar()) to print each character. This is a lot more efficient than P.MID$("0*+",p MOD3+1,1).

Here it is running in BeebEm3 on a Mac:

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ How you create that gif? \$\endgroup\$ Commented Sep 10, 2015 at 16:45
  • 9
    \$\begingroup\$ @JuanCarlosOropeza Not very efficiently. I used QuickTime Player to capture the screen, QuickTime Player 7 to export the video to PNG images, GraphicConverter to turn them into a GIF, and ezgif.com to optimize the results. \$\endgroup\$
    – r3mainer
    Commented Sep 10, 2015 at 18:17
7
\$\begingroup\$

CJam, 25 bytes

q~,_1>W%\+_ff{e>"0*+"=}N*

Test it here.

Explanation

q~,       e# Read input N, turn into range [0 1 ... N-1]
_1>       e# Duplicate and cut off the zero.
W%        e# Reverse.
\+        e# Prepend to original range to give [N-1 ... 1 0 1 ... N-1]
_         e# Duplicate
ff{       e# Nested map for each pair of elements in that array.
  e>      e# Take the maximum, i.e. chessboard distance from the centre.
  "0*+"=  e# Select the right character using cyclic indexing into this string.
}
N*        e# Join the lines with line feeds.
\$\endgroup\$
7
\$\begingroup\$

K5, 27 30 26 25 22 bytes

"0"{4(|+y,)/x}/"0*+"3!1_!

This approach iteratively "wraps" a core (beginning with "0") on all four sides using some other character ({4(|+y,)/x}). The sequence of seasonal wrappings is determined by a modulo 3 (3!) sequence. It's a bit fiddly to get the base case to line up just right.

edit:

"0*+"3!u|\:u:t,1_|t:|!

This alternative builds the entire rectangular array at once from the provided exclusive range (!) reversed and joined with itself after dropping an item (t,1_|t:|). We then take the cartesian product maximum (u|\:u:), take the entire matrix modulo 3 (3!) and index into the array of characters.

In action:

  "0*+"3!u|\:u:t,1_|t:|!1
,,"0"

  "0*+"3!u|\:u:t,1_|t:|!3
("+++++"
 "+***+"
 "+*0*+"
 "+***+"
 "+++++")

  "0*+"3!u|\:u:t,1_|t:|!5
("*********"
 "*0000000*"
 "*0+++++0*"
 "*0+***+0*"
 "*0+*0*+0*"
 "*0+***+0*"
 "*0+++++0*"
 "*0000000*"
 "*********")
\$\endgroup\$
2
  • \$\begingroup\$ I don't know K, but is this a full program and not just a function? \$\endgroup\$
    – Alex A.
    Commented Sep 9, 2015 at 18:27
  • \$\begingroup\$ It is both a full program and a function. It's an example of what is called a "tacit definition". The distinction is extremely arbitrary anyway. \$\endgroup\$
    – JohnE
    Commented Sep 9, 2015 at 19:10
6
\$\begingroup\$

Python 2, 83 bytes

I=n=input()
while I+n-1:I-=1;i=abs(I);w=("O*+"*n)[i:n];print w[::-1]+w[0]*2*i+w[1:]

Prints line by line. Each line is chopped into three parts:

  • The left cycling part, including the first repeated char.
  • The repeating center part
  • The right cycling part.

For n=4:

0    000000    
0+    ++++    0
0+*    **    +0
0+*0        *+0
0+*    **    +0
0+    ++++    0
0    000000    

We generate the left part in reverse as w, clone its last character 2*i times, then add on the original version without the first character.

\$\endgroup\$
5
\$\begingroup\$

Matlab, 63 bytes

n=input('')-1;x='0*+';t=abs(-n:n);x(mod(bsxfun(@max,t,t'),3)+1)

Example:

>> n=input('')-1;x='0*+';t=abs(-n:n);x(mod(bsxfun(@max,t,t'),3)+1)
5
ans =
*********
*0000000*
*0+++++0*
*0+***+0*
*0+*0*+0*
*0+***+0*
*0+++++0*
*0000000*
*********
\$\endgroup\$
5
\$\begingroup\$

Python 2, 83 bytes

n=input()
R=range(1-n,n)
for i in R:print''.join('0*+'[max(i,-i,j,-j)%3]for j in R)

If we think of the tree as a coordinate grid, the symbol at (i,j) is determined by max(abs(i),abs(j))%3, or equivalently max(i,-i,j,-j)%3. For each row i, we join and print the symbols in that row.

\$\endgroup\$
3
  • \$\begingroup\$ You could shorten this by putting the range statement straight in the third line. \$\endgroup\$ Commented Sep 9, 2015 at 23:34
  • \$\begingroup\$ @EthanBrouwer I'm using R twice, and it's longer than 5 chars, so assignment wins out. \$\endgroup\$
    – xnor
    Commented Sep 9, 2015 at 23:40
  • \$\begingroup\$ touche! I only saw the first one. My bad. :) \$\endgroup\$ Commented Sep 10, 2015 at 2:49
5
\$\begingroup\$

Pyth, 23 bytes

VK+_StQUQsm@"0*+"eS,dNK

Try it online: Demonstration

Explanation:

VK+_StQUQsm@"0*+"eS,dNK   implicit: Q = input number
    StQ                   the list [1, 2, ..., Q-1]
   _                      reverse it [Q-1, ..., 2, 1]
       UQ                 the list [0, 1, ..., Q-1]
  +                       combine them [Q-1, ..., 1, 0, 1, ..., Q-1]
 K                        and store in K
VK                        for each N in K:
          m           K      map each element d in K to:
                 eS,dN          the maximum of d and N
           @"0*+"               and pick the corresponded char (modulo 3)
         s                   join the chars to a string and print
\$\endgroup\$
3
\$\begingroup\$

MATLAB, 80 78 73 bytes

Thanks Luis Mendo for helping me shave 5 bytes!

A=eye(2*input('')-1);a='0*+';a(mod(bwdist(A.*rot90(A),'chessboard'),3)+1)

Example

>> A=eye(2*input('')-1);a='0*+';a(mod(bwdist(A.*rot90(A),'chessboard'),3)+1)

5

ans =

*********
*0000000*
*0+++++0*
*0+***+0*
*0+*0*+0*
*0+***+0*
*0+++++0*
*0000000*
*********

Ungolfed and Code Explanation

%// Accepts an integer n from the user and creates a 2*n - 1 x 2*n - 1 identity matrix
A=eye(2*input('')-1);

%// Creates an array of three characters to print each level of the ring
a='0*+';

%// By taking the identity matrix and element-wise multiplying with its 90 degree rotated 
%// version of itself, this creates a zero matrix except for the centre most
%// value, which is 1
%// This takes the distance transform via the chessboard / Chebyshev distance
%// from the centre element
%// This mirrors what "level" each square would be at
%// 1: https://en.wikipedia.org/wiki/Distance_transform
%// 2: https://en.wikipedia.org/wiki/Chebyshev_distance
b = bwdist(A.*rot90(A),'chessboard');

%// Because each level cycles through each of the characters in the
%// character array a, we need to perform a mod operation so that
%// all of the values cycle from 1 to 3
%// This changes the distance transform output so that we range
%// from 1 to 3 instead
c = mod(b,3) + 1;

%// The values in the matrix c correspond exactly to the locations
%// we need to sample from the array a and we display our result
a(c)

Minor Note

bwdist is a function that's part of the image processing toolbox, and can only be run in MATLAB. Octave (IIRC) does not have bwdist implemented yet so this can't be run in Octave.

\$\endgroup\$
2
  • \$\begingroup\$ You can save a few bytes: use eye and multiply element-wise by its rot90'ed version to generate the "seed" matrix: I=eye(2*input('')-1);a='0*+';a(mod(bwdist(I.*rot90(I),'chessboard'),3)+1) \$\endgroup\$
    – Luis Mendo
    Commented Sep 10, 2015 at 11:38
  • \$\begingroup\$ Oh cool! Thanks @LuisMendo \$\endgroup\$
    – rayryeng
    Commented Sep 10, 2015 at 12:22
3
\$\begingroup\$

Charcoal, 15 14 bytes

FN«B⊕⊗ι§0*+ιM↖

Try it online!

explanation

FN                 #start a for loop, iterating n (input) times
   «                 #the loop contains 2 expressions so a body is opened
    B               #draw a box
      ⊕⊗ι          #size of box is 2*i+1
           §0*+ι     #character of the box is "0*+" index i
                M↖  #move up left 1
                     #the result is printed implicitly

15 bytes

FN«B⊕ײι§0*+ιM↖

Improved version by Psuedo Nym replaced ײ with , the in-built doubled operator.

\$\endgroup\$
1
  • \$\begingroup\$ -1 byte: FN«B⊕⊗ι§0*+ιM↖ \$\endgroup\$
    – Pseudo Nym
    Commented Dec 19, 2019 at 13:16
2
\$\begingroup\$

Pyke, 13 bytes

0QV"0*+"[email protected]

Based on this excellent Pyke answer on another challenge, so make sure to upvote that one as well!

Try it online.

Explanation:

0              # Start with a "0" in the center
 QV            # Loop the input amount of times:
   "0*+"       #  Push string "0*+"
        o      #  Push index++ (0 by default; and increases by 1 after pushing)
         @     #  And use it to index it into the string (0-based with automatic wraparound)
          A    #  Deep apply the following command:
           .X  #   Surround the string with this character around it
               # (after which the result is output implicitly)
\$\endgroup\$
2
\$\begingroup\$

Python 2, 134 bytes

def l(x,c=1):
 p="\n\x1b[%d"%c;d=p+";%dH"%c
 if x:s=x*2-1;d+=(p+"G").join(["0*+"[(x+1)%3]*s]*s)+l(x-1,c+1)
 return d
print l(input())
\$\endgroup\$
0
2
\$\begingroup\$

Perl 5, 57 bytes

Using the approach from @xnor's answer, please upvote them!

//,say map{qw(0 * +)[max(abs,abs$')%3]}@;for@;=- --$_..$_

Try it online!

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

Matlab 92

input('')-1;x=ones(2*n+1,1)*abs(-n:n);z=mod(max(x,x'),3);z(z>1)=2;z(z<1)=7;disp([z+41,''])
\$\endgroup\$
1
\$\begingroup\$

Sed, 277 252 characters

(251 character code + 1 character command line option.)

Expects input in unary format.

:m
s/1/0/
s/1/*/
s/1/+/
tm
h
s/^/:/
:r
s/(.*):(.)/\2\1:/
tr
s/://
G
s/\n.//
h
:
/^(.)\1*$/ba
s/(.)(.)(\2*)\1/\1:\2\3:\1/
:c
s/(:_*)[^_](.*:)/\1_\2/
tc
:u
s/(.)(:\1*)_/\1\2\1/
tu
s/://g
H
b
:a
g
s/[^\n]+/:/
:f
s/(.*):(\n[^\n]+)/\2\1:/
tf
s/://
G
s/\n//

Sample run:

bash-4.3$ sed -rf treering.sed <<< 1
0

bash-4.3$ sed -rf treering.sed <<< 11
***
*0*
***

bash-4.3$ sed -rf treering.sed <<< 111
+++++
+***+
+*0*+
+***+
+++++

bash-4.3$ sed -rf treering.sed <<< 1111
0000000
0+++++0
0+***+0
0+*0*+0
0+***+0
0+++++0
0000000
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 68 bytes

$f={($l=($c='+0*'[$_--%3])+"$c$c"*$_)
if($_){&$f $_|%{"$c$_$c"}
$l}}

Try it online!

Unrolled:

$f={
    $_--
    $char='+0*'[$_%3]
    $line=$char+"$char$char"*$_
    Write-Output $line
    if($_){
        &$f $_|ForEach-Object{
            Write-Output "$char$_$char"
        }
        Write-Output $line
    }
}
\$\endgroup\$
1
\$\begingroup\$

Canvas, 15 bytes

H0*+@¹2×╷:*;22╋

Try it here!

Can probably be shortened further.

Explanation

H0*+@¹2×|:*;22╋
H               push an empty art object, start a loop using input
 0*+            string "0*+"
    @           get the nth element, circularly <-----------+
     ¹          loop iteration                              |
      2×        double it                                   |
        |       decrement it                                |
         :      duplicate it                                |
          *     use that to box the character from here ----+
           ;    swap the box with the previous iteration
            22╋ overlap at (2,2) 
\$\endgroup\$
1
\$\begingroup\$

Japt -h, 20 bytes

Ñ ÆP=Õû"0*+"gX=z)XÑÄ

Try it or run all test cases

Ñ ÆP=Õû"0*+"gX=z)XÑÄ     :Implicit input of integer U
Ñ                        :U*2
  Æ                      :Map each X in the range [0,U*2)
   P=                    :  Reassign to P (initially the empty string)
     Õ                   :  Transpose P
      û                  :  Centre pad with
       "0*+"             :    Literal string
            g            :    Get character at 0-based index
             X=          :      Reassign to X
               z         :      X floor divided by 2
                )        :    End get
                 XÑÄ     :    To length X*2+1
\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 114

Using alert for output - bad proportional font and the result is ugly. In the snippet below the alert is redirect to the snipped body, giving a better result. The newline inside backticks is significant and counted.

Test running the snippet in Firefox.

/* Redefine alert for testing purpose */ alert=x=>O.innerHTML=x;

[...'*+0'.repeat(n=prompt()-1)].map((c,i)=>i<n?b=[z=c.repeat(i-~i),...b,z].map(r=>c+r+c):0,b=[0]);alert(b.join`
`)
<pre id=O></pre>

\$\endgroup\$
3
  • \$\begingroup\$ I try run the code snippet, but nothing happen. Dont know if that is because I open stack overflow in chrome? \$\endgroup\$ Commented Sep 10, 2015 at 16:46
  • \$\begingroup\$ @JuanCarlosOropeza maybe that. I have written: Test running the snippet in Firefox but obviously I was just kidding, Chrome (no version of Chrome) is not EcmaScritpt 6 compliant, missing the => functions. \$\endgroup\$
    – edc65
    Commented Sep 10, 2015 at 17:49
  • \$\begingroup\$ @JuanCarlosOropeza I have to correct myself. The newest release of Chrome has the arrow function, but does not understand the spread operator .... Still far from ES6 \$\endgroup\$
    – edc65
    Commented Sep 13, 2015 at 14:04
0
\$\begingroup\$

Ruby, 85 characters

puts (m=0..(n=gets.to_i-1)*2).map{|i|m.map{|j|"0*+"[[(i-n).abs,(j-n).abs].max%3]}*""}

Sample run:

bash-4.3$ ruby -e 'puts (m=0..(n=gets.to_i-1)*2).map{|i|m.map{|j|"0*+"[[(i-n).abs,(j-n).abs].max%3]}*""}' <<< 4
0000000
0+++++0
0+***+0
0+*0*+0
0+***+0
0+++++0
0000000
\$\endgroup\$
0
\$\begingroup\$

Moonscript - 104 bytes

m=io.read!
n=2*m-1
for y=1,n
 io.write ({'0','*','+'})[(math.max y-m,x-m,m-y,m-x)%3+1]for x=1,n
 print!
\$\endgroup\$
0
\$\begingroup\$

C, 138 bytes

j,k,l;t(i){l=2*i;char*c=calloc(l,l);memset(c,10,l*(l-2));for(;k<i;++k)for(j=k;j<l-1-k;)memset(c+j++*l+k,"+0*"[(i-k)%3],l-2*k-1);puts(c);}

Function t taking one integer parameter - the age.

Ungolfed (with main function to easily run the above one):

#include "stdlib.h" /* calloc - only necessary for 64-bit system */
j,k,l;t(i)
{
    l=2*i;
    char*c=calloc(l,l);
    memset(c,10,l*(l-2)); /* fill with '\n' */
    for(;k<i;++k)for(j=k;j<l-1-k;)memset(c+j++*l+k,"+0*"[(i-k)%3],l-2*k-1);
    puts(c);
}

main(int c,char**v)
{
    t(atoi(v[1]));
}

The stdlib.h may be necessary on some systems, because without it the return type of the undeclared function calloc would default to int. Because int and char* are not necessarily of same size, an invalid pointer could be written into c. In most 32-bit systems both char* and int have same size, but this is not true for 64-bit systems.

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

05AB1E, 23 bytes

0¸sG…0*+Nè©δ.ø®N·>×.ø}»

Try it online or verify a few more test cases.

Explanation:

0¸                     # Start with a 0 wrapped in a list as the center: [0]
  sG                   # Loop `N` in the range [1, input):
    …0*+               #  Push string "0*+"
        Nè             #  Use `N` to index into this string
                       #  (0-based with automatic wraparound)
          ©            #  Store this character in variable `®` (without popping)
           δ.ø         #  Surround each line with this character
               N·>     #  Push 2 * `N` + 1
              ®   ×    #  Repeat the character `®` that many times as string
                   .ø  #  And surround the list with this string
   }»                  # After the loop: join all strings by newlines
                       # (after which the resulting string is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

PowerShell, 118 bytes

param($n)((--$n..0+1..$n|%{-join(($z=($x='0*+'*$n)[$n..$_])+"$($x[$_])"*2*$_+($z[($n-$_-1)..0],'')[$n-eq$_])}),0)[!$n]

Try it online!

:(

\$\endgroup\$
2
  • 1
    \$\begingroup\$ recursion rulez :) \$\endgroup\$
    – mazzy
    Commented Dec 18, 2019 at 21:14
  • 1
    \$\begingroup\$ @mazzy You sure you don't want to post that as your own? \$\endgroup\$
    – Veskah
    Commented Dec 19, 2019 at 4:14
0
\$\begingroup\$

J, 22 bytes

'0*+'{~3|>./~&:|@i:@<:

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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