# Help my maniacal wife decorate our Christmas tree

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.

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).

  ^
^ ^
^ ^ ^


### 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!

• spaces after each line acceptable ? Commented Dec 6, 2016 at 13:44
• @MukulKumar no It should maintain the structure above. Commented Dec 6, 2016 at 13:47
• Can we assume k less than 10? Or else, how to align the numbers? Commented Dec 6, 2016 at 15:28
• @LuisMendo Yes assume < 10 good point Commented Dec 6, 2016 at 15:43

# 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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^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
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 :)

MerryChristmas.gif

• You can give i=1 in the int declaration and remove it from your for loop... Commented Dec 7, 2016 at 1:58
• You can replace "\r\n" with "\n". It will run fine with core and mono. Commented Dec 7, 2016 at 9:22
• Good to see a C# answer getting some love. Commented Dec 7, 2016 at 15:23
• @jacksonecac Glad you enjoyed it. Couldn't resist once the idea came into my head :) Commented Dec 9, 2016 at 20:46
• @MichaelMcGriff Just when I was starting to think I would never succeed at Code Golf using C#... :) Commented Dec 9, 2016 at 20:47

# 05AB1E, 2927 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!

• I think you can replace NN>*; by NLO. Commented Dec 7, 2016 at 10:20
• 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! Commented Dec 7, 2016 at 10:36
• Also, do you need the ï part now :p? Commented Dec 7, 2016 at 10:50
• I don't think so ^^ . Commented Dec 7, 2016 at 10:51

## 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))

# 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
}

• finally < 200... Commented Dec 6, 2016 at 14:58
• Can you remove the space after #define s(a)? Commented Dec 7, 2016 at 0:19
• @Cyoce thanks! I did not know about that!!! Commented Dec 7, 2016 at 4:54
• 186 bytes by changing the define to #define s std::cout<< and making according adjustments. Commented Dec 7, 2016 at 22:20
• Woaa..thats nice.. never crossed my mind:p Commented Dec 8, 2016 at 4:09

## 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+1for x in range(d,d+i))+'\n'][i>1]+s+j('^'*i));d+=i


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.

• Nice first answer, welcome to the site! Commented Dec 8, 2016 at 20:32

## 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
^ ^ ^ ^ ^


# 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);}}