Code Golf Christmas Edition: How to print out a Christmas tree of height N
Brainfuck, 240 characters
,
>++
+++++
+[-<---
--->],[>+
+++++++[-<-
----->]<<[->+
+++++++++<]>>]<
[->+>+>>>>>>>+<<<
<<<<<<]>>>>++++++++
[-<++++>]>++++++[-<++
+++++>]+>>>++[-<+++++>]
<<<<<<[-[>.<-]<[-<+>>+<]<
[->+<]>>>>>[-<.>>+<]>[-<+>]
>.<<++<<<-<->]>>>>>>>-[-<<<<<
<.>>>
>>>]<
<<<<.
Not yet done. It works, but only with single-digit numbers.
EDIT: Done! Works for interpreters using 0 as EOF. See NOTE
s in commented source for those with -1.
EDIT again: I should note that because Brainfuck lacks a standard method for reading command line arguments, I used stdin (standard input) instead. ASCII, of course.
EDIT a third time: Oh dear, it seems I stripped .
(output) characters when condensing the code. Fixed...
Here's the basic memory management of the main loop. I'm sure it can be heavily optimized to reduce the character count by 30 or so.
- Temporary
- Copy of counter
- Counter (counts to 0)
- Space character (decimal 32)
- Asterisk character (decimal 42)
- Number of asterisks on current line (1 + 2*counter)
- Temporary
- New line character
- Temporary?
- Total number of lines (i.e. input value; stored until the very end, when printing the trunk)
Condensed version:
,>++++++++[-<------>],[>++++++++[-<------>]<<[->++++++++++<]>>]<[->+>+>>>>>>>+<<<<<<<<<]>>>>++++++++[-<++++>]>++++++[-<+++++++>]+>>>++[-<+++++>]<<<<<<[-[>.<-]<[-<+>>+<]<[->+<]>>>>>[-<.>>+<]>[-<+>]>.<<++<<<-<->]>>>>>>>-[-<<<<<<.>>>>>>]<<<<<.
And the pretty version:
ASCII to number
,>
++++++++[-<------>] = 48 ('0')
Second digit (may be NULL)
,
NOTE: Add plus sign here if your interpreter uses negative one for EOF
[ NOTE: Then add minus sign here
>++++++++[-<------>]
<<[->++++++++++<]>> Add first digit by tens
]
Duplicate number
<[->+>+>>>>>>>+<<<<<<<<<]>>
Space char
>>++++++++[-<++++>]
Asterisk char
>++++++[-<+++++++>]
Star count
+
New line char
>>>++[-<+++++>]<<<
<<<
Main loop
[
Print leading spaces
-[>.<-]
Undo delete
<[-<+>>+<]
<[->+<]
>>
Print stars
>>>[-<.>>+<]
Add stars and print new line
>[-<+>]
>.<
<++
<<<
-<->
End main loop
]
Print the trunk
>>>>>>>
-[-<<<<<<.>>>>>>]
<<<<<.
Merry Christmas =)
Perl, 50 chars
(1 relevant spaces)
perl: one line version:
print$"x($a-$_),'*'x($_*2+1),$/for 0..($a=pop)-1,0
and now with more whitesapce:
print $" x ( $a - $_ ), #"# Syntax Highlight Hacking Comment
'*' x ( $_ * 2 + 1),
$/
for 0 .. ( $a = pop ) - 1, 0;
$ perl tree.pl 3
*
***
*****
*
$ perl tree.pl 11
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*
$
Expanded Explanation for Non-Perl Users.
# print $Default_List_Seperator ( a space )
# repeated ( $a - $currentloopiterationvalue ) times,
print $" x ( $a - $_ ),
#"# print '*' repeated( $currentloopiteration * 2 + 1 ) times.
'*' x ( $_ * 2 + 1),
# print $Default_input_record_seperator ( a newline )
$/
# repeat the above code, in a loop,
# iterating values 0 to ( n - 1) , and then doing 0 again
for 0 .. ( $a = pop ) - 1, 0;
# prior to loop iteration, set n to the first item popped off the default list,
# which in this context is the parameters passed on the command line.
J, 24 characters
(,{.)(}:@|."1,.])[\'*'$~
(,{.)(}:@|."1,.])[\'*'$~5
*
***
*****
*******
*********
*
Explanation:
'*'$~5
*****
[\'*'$~5
*
**
***
****
*****
Then }:@|."1
reverses each row and strips off the last column, and ,.
staples it to ]
.
Then ,{.
pastes the first column onto the bottom.
Previous entries:
29 characters, no spaces at all.
((\:i.@#),}.)"1$&'*'"0>:0,~i.3 * *** ***** * ((\:i.@#),}.)"1$&'*'"0>:0,~i.11 * *** ***** ******* ********* *********** ************* *************** ***************** ******************* ********************* *
NB. count from 1 to n, then 1 again >:0,~i.3 1 2 3 1 NB. replicate '*' x times each $&'*'"0>:0,~i.3 * ** *** * NB. reverse each row (\:i.@#)"1$&'*'"0>:0,~i.3 * ** *** * NB. strip off leading column }."1$&'*'"0>:0,~i.3 * ** NB. paste together ((\:i.@#),}.)"1$&'*'"0>:0,~i.3 * *** ***** *