Help my maniacal wife decorate our Christmas tree

36

6

My wife is very, let's say, particular when it comes to putting ornaments on our Christmas tree. Let's get her some code to help her in this trying time.

Input

Given an input 2 < n < 10 the height of the tree and 0 < k < n the distinct number of ornaments.

Task

Decorate the tree starting with 1 and increment to k as we wrap the ornaments around the tree. If we reach k and we have more branches to decorate then start back at 1.

It is okay if there are not the same number of each ornament on the tree, as long as the pattern is satisfied.

The ornaments should appear above each branch ^ except for the top row.

The tree is structured by starting with one branch then the next level has + 1 branches with a space between each, staggered from the top like:

 ^
^ ^

For a third row you would add one more branch and stagger them again such that no branch is on the same column (if you think of it like a grid).

  ^
 ^ ^
^ ^ ^

Output

Output your decorated tree.

Examples

1.

n = 3, k = 2

  ^      //Height is 3
 ^ ^
^ ^ ^

Now we decorate each branch starting with 1 and increment to k:

  ^
 1 2
 ^ ^
1 2 1
^ ^ ^

2.

n = 6, k = 5

     ^    //Non-Decorated
    ^ ^
   ^ ^ ^
  ^ ^ ^ ^
 ^ ^ ^ ^ ^
^ ^ ^ ^ ^ ^

     ^    //Decorated
    1 2
    ^ ^
   3 4 5
   ^ ^ ^
  1 2 3 4
  ^ ^ ^ ^
 5 1 2 3 4
 ^ ^ ^ ^ ^
5 1 2 3 4 5
^ ^ ^ ^ ^ ^

3.

n = 5, k = 1

    ^
   ^ ^
  ^ ^ ^
 ^ ^ ^ ^
^ ^ ^ ^ ^

    ^
   1 1
   ^ ^
  1 1 1
  ^ ^ ^
 1 1 1 1
 ^ ^ ^ ^
1 1 1 1 1
^ ^ ^ ^ ^

This is so the shortest code wins! Have fun and good luck!

jacksonecac

Posted 2016-12-06T12:38:55.527

Reputation: 2 584

spaces after each line acceptable ? – Mukul Kumar – 2016-12-06T13:44:35.473

1@MukulKumar no It should maintain the structure above. – jacksonecac – 2016-12-06T13:47:43.187

Can we assume k less than 10? Or else, how to align the numbers? – Luis Mendo – 2016-12-06T15:28:53.953

2@LuisMendo Yes assume < 10 good point – jacksonecac – 2016-12-06T15:43:28.627

Answers

47

C# 226 221 Bytes

Saved 5 Bytes thanks to @Mukul Kumar and @aloisdg

Golfed:

string C(int n,int k){string o="",x;int j=1,i=1,m;for(;i<=n;i++){o+=string.Concat(Enumerable.Repeat("^ ",i)).PadLeft(n+i)+"\n";m=0;x="";if(i<n){while(m<i+1){if(j>k)j=1;x+=j+++" ";m++;}o+=x.PadLeft(n+i+1)+"\n";}}return o;}

Ungolfed:

public string C(int n, int k, WifeMode wifeMode = WifeMode.Maniacal)
{
  string o = "",x;
  int j = 1,i=1,m;

  for (; i <= n; i++)
  {
    o += string.Concat(Enumerable.Repeat("^ ", i)).PadLeft(n+i) + "\n";

    m = 0;
    x = "";

    if (i < n)
    {
      while (m < i + 1)
      {
        if (j > k) j = 1;
        x += j++ + " ";
        m++;
      }

      o += x.PadLeft(n + i + 1) + "\n";
    }
  }

  return o;
}

Testing:

Console.Write(new ChristmasTreeDecorating().C(20, 9));

                   ^ 
                  1 2 
                  ^ ^ 
                 3 4 5 
                 ^ ^ ^ 
                6 7 8 9 
                ^ ^ ^ ^ 
               1 2 3 4 5 
               ^ ^ ^ ^ ^ 
              6 7 8 9 1 2 
              ^ ^ ^ ^ ^ ^ 
             3 4 5 6 7 8 9 
             ^ ^ ^ ^ ^ ^ ^ 
            1 2 3 4 5 6 7 8 
            ^ ^ ^ ^ ^ ^ ^ ^ 
           9 1 2 3 4 5 6 7 8 
           ^ ^ ^ ^ ^ ^ ^ ^ ^ 
          9 1 2 3 4 5 6 7 8 9 
          ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
         1 2 3 4 5 6 7 8 9 1 2 
         ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
        3 4 5 6 7 8 9 1 2 3 4 5 
        ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
       6 7 8 9 1 2 3 4 5 6 7 8 9 
       ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
      1 2 3 4 5 6 7 8 9 1 2 3 4 5 
      ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
     6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 
     ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
    3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 
    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
   1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 
   ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
  9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 
  ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 
 ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

Edit: I had a play casting int to ConsoleColor... Tis the season :)

enter image description here

MerryChristmas.gif

enter image description here

Pete Arden

Posted 2016-12-06T12:38:55.527

Reputation: 1 151

3You can give i=1 in the int declaration and remove it from your for loop... – Mukul Kumar – 2016-12-07T01:58:53.083

1You can replace "\r\n" with "\n". It will run fine with core and mono. – aloisdg moving to codidact.com – 2016-12-07T09:22:31.590

8Good to see a C# answer getting some love. – Michael McGriff – 2016-12-07T15:23:42.380

@aloisdg Thanks :) – Pete Arden – 2016-12-09T20:45:53.713

1@jacksonecac Glad you enjoyed it. Couldn't resist once the idea came into my head :) – Pete Arden – 2016-12-09T20:46:35.747

@MichaelMcGriff Just when I was starting to think I would never succeed at Code Golf using C#... :) – Pete Arden – 2016-12-09T20:47:04.743

@tuskiomi Thanks :) – Pete Arden – 2016-12-09T21:05:53.097

14

05AB1E, 29 27 24 bytes

Saved three bytes thanks to Adnan!

>GN„^ ×NÝNLO<+²%>ðý}\».c

>G                       For N in [1, ..., input[0]]
  N„^ ×                  Push a string of "^ " N times
       NÝ                Push [0, ..., N]
         NLO<            Compute the decoration offset, sum([1, ..., N])-1
             +           Add the offset value to each array cell
              ²%         Modulo input[1]
                >        Add 1 so that it is in range [1, k] instead of [0, k-1]
                 ðý      Join with spaces, now we have a string with the full decoration for the current layer
                   }     End for
                    \    Remove the last decoration
                     »   Join everything with newlines
                      .c Center all and implicitly display

Try it online!

Osable

Posted 2016-12-06T12:38:55.527

Reputation: 1 321

2I think you can replace NN>*; by NLO. – Adnan – 2016-12-07T10:20:07.990

Of course! That's funny because I actually used N*(N+1)/2 intentionally to get the sum of consecutive integers starting from 1, but I completely forgot that 05AB1E had built-ins for that. Thanks! – Osable – 2016-12-07T10:36:03.723

2Also, do you need the ï part now :p? – Adnan – 2016-12-07T10:50:25.560

1I don't think so ^^ . – Osable – 2016-12-07T10:51:20.980

12

JavaScript (ES6), 97 bytes

It seems like your wife really is maniacal, so this doesn't include any leading or trailing newline, nor any leading or trailing space. :-)

f=(n,k,x,s=`^
`)=>n--?(p=' '.repeat(n)+s,x?p.replace(/\^/g,_=>x++%k+1)+p:p)+f(n,k,x||k,'^ '+s):''

Demo

f=(n,k,x,s=`^
`)=>n--?(p=' '.repeat(n)+s,x?p.replace(/\^/g,_=>x++%k+1)+p:p)+f(n,k,x||k,'^ '+s):''

console.log(f(6, 5))

Arnauld

Posted 2016-12-06T12:38:55.527

Reputation: 111 334

8

C++ 214 - 13 - 3 - 1 -1 - 10 = 186 bytes

golfed

#define s std::cout<<
int f(int n,int k){int N=++n,K=0,i=0,I;for(;i<n;i++,N--){for(I=N;I--;)s' ';for(I=0;I++<i&&i-1;)s' '<<(K++%k)+1;s'\n';for(I=N;I--;)s' ';for(I=0;I++<i;)s" ^";s'\n';}}  

Thanks @ cyoce for saving 1 byte.
Thanks @ conor for chopping it down to 186!

Ungolfed + copy&compile

#include<iostream>
#include<conio.h>

#define s(a) std::cout<<a;

int main()
{
    int n,N,k,K=0,i,I;
    std::cin>>n>>k;
    N=++n;
    for(i=0;i<n;i++,N--)
    {
        for(I=N;I--;)

            s(' ')

        for(I=0;I<i&&i-1;I++)

            s(' '<<(K++%k)+1)

        s('\n')

        for(I=N;I;I--)

            s(' ')

        for(I=0;I<i;I++)

            s(" ^")

        s('\n')

    }
    getch();//or any func to pause the console
}  

Mukul Kumar

Posted 2016-12-06T12:38:55.527

Reputation: 2 585

finally < 200... – Mukul Kumar – 2016-12-06T14:58:28.580

1Can you remove the space after #define s(a)? – Cyoce – 2016-12-07T00:19:11.980

@Cyoce thanks! I did not know about that!!! – Mukul Kumar – 2016-12-07T04:54:29.313

2186 bytes by changing the define to #define s std::cout<< and making according adjustments. – Conor O'Brien – 2016-12-07T22:20:26.063

Woaa..thats nice.. never crossed my mind:p – Mukul Kumar – 2016-12-08T04:09:43.230

3

Python 2, 133 bytes

n,k=input()
d=-1
j=' '.join
for i in range(1,n+1):s=' '*(n-i);print(['',s+j(`x%k+1`for x in range(d,d+i))+'\n'][i>1]+s+j('^'*i));d+=i

TFeld

Posted 2016-12-06T12:38:55.527

Reputation: 19 246

2

Clojure, 223 bytes

My first go at golfing with Clojure:

(let[r repeat](defn d[n k](apply str(concat(r(dec n)\ )"^\n"(flatten(for[i(range 2(inc n))m[nil true]](concat(r(- n i)\ )(butlast(interleave(if m(r\^)(rest(iterate #(inc(mod % k))(dec(/(* i(dec i))2)))))(r i\ )))"\n")))))))

When called like (println (str "\n" (d 6 5))) a newline makes it nicer on REPL:

     ^
    1 2
    ^ ^
   3 4 5
   ^ ^ ^
  1 2 3 4
  ^ ^ ^ ^
 5 1 2 3 4
 ^ ^ ^ ^ ^
5 1 2 3 4 5
^ ^ ^ ^ ^ ^

Un-golfed:

(defn tree-row [n k index mode]
  (concat
    (repeat (- n index) \ ) ; Left padding
    (butlast ; Removing trailing space
      (interleave
        ; Either printing carets or numbers...
        (if mode
          (repeat \^)
          ; Using "rest" as the iteration starts from a large value
          ; from which the modulo has not been calculated yet.
          (rest (iterate #(inc (mod % k)) (dec (/ (* index (dec index)) 2)))))
        ; ...and interleaved with spaces
        (repeat index \ )))
    "\n"))

(defn decorate [n k]
  (apply str (concat
               (repeat (dec n) \ ) "^\n"
               (flatten (for [index (range 2 (inc n)) mode [nil true]]
                          (tree-row n k index mode))))))

I had some issues with lazy sequences and nested lists but I was able to save some characters by not repeating repeat ;) and using \^ characters instead of "^" strings. I could also leave out surprisingly many spaces.

NikoNyrh

Posted 2016-12-06T12:38:55.527

Reputation: 2 361

Nice first answer, welcome to the site! – James – 2016-12-08T20:32:15.203

1

Ruby 107 bytes

t=->(n,k){d=[*1..k]*n*n;o=0;(1..n).each{|b|s=' '*(n-b);b>1&&(puts(s+d[o,b].join(' '));o+=b);puts s+'^ '*b}}

Called like this

t.call(5,4)

Output:

    ^
   1 2
   ^ ^
  3 4 1
  ^ ^ ^
 2 3 4 1
 ^ ^ ^ ^
2 3 4 1 2
^ ^ ^ ^ ^

Neil Slater

Posted 2016-12-06T12:38:55.527

Reputation: 261

1

C, 170 bytes

i=0;d,j,l;t(n,k){char s[20],r[20];d=k-2;l=n;for(;i++<n;){for(j=0;j<l;++j)s[j]=r[j]=32;for(j=n-i;j<l;j+=2){s[j]=94;r[j]=(++d%k)+49;}s[l]=r[l++]=0;if(i-1)puts(r);puts(s);}}

Call with:

int main()
{
   t(5,4);
}

As a bonus, here's a 4-bit binary version:

m=0;b(n,k){char*a="000100100011010001010110011110001001";char s[20],r[20];d=k*4-2;l=n;for(;m++<n;){for(j=0;j<l;++j)s[j]=r[j]=32;for(j=n-m;j<l;j+=2){s[j]=94;r[j]=a[++d%(k*4)];}s[l]=r[l++]=0;if(m-1)puts(r);puts(s);}}

Steadybox

Posted 2016-12-06T12:38:55.527

Reputation: 15 798