48
votes

This is inspired by/taken from this thread: http://www.allegro.cc/forums/thread/603383

The Problem

Assume the user gives you a numeric input ranging from 1 to 7. Input should be taken from the console, arguments are less desirable.

When the input is 1, print the following:

***********
 ********* 
  *******    
   *****   
    ***    
     *     

Values greater than one should generate multiples of the pattern, ending with the one above, but stacked symmetrically. For example, 3 should print the following:

*********** *********** ***********
 *********   *********   ********* 
  *******     *******     *******  
   *****       *****       *****   
    ***         ***         ***    
     *           *           *     
      *********** ***********
       *********   ********* 
        *******     *******  
         *****       *****   
          ***         ***    
           *           *     
            ***********
             ********* 
              *******  
               *****   
                ***    
                 *     

Bonus points if you print the reverse as well.

      *********** ***********
       *********   ********* 
        *******     *******  
         *****       *****   
          ***         ***    
           *           *     
            ***********
             ********* 
              *******  
               *****   
                ***    
                 *     
                 *     
                ***    
               *****   
              *******  
             ********* 
            ***********
           *           *     
          ***         ***    
         *****       *****   
        *******     *******  
       *********   ********* 
      *********** ***********

Can we try and keep it to one answer per language, that we all improve on?

0

23 Answers 23

38
votes

Assembler, 165 bytes assembled

Build Instructions

  1. Download A86 from here
  2. Add a reference to the A86 executable into your DOS search path
  3. Paste the code below into a text file (example: triforce.asm)
  4. Invoke the assembler: a86 triforce.asm
  5. This will create a .COM file called triforce.com
  6. Type triforce to run

This was developed using the standard WinXP DOS box (Start->Programs->Accessories->Command Prompt). It should work with other DOS emulators.

Assemble using A86 and requires WinXP DOS box to run the .COM file it produces. Press 'q' to exit, keys 1-7 to draw the output.

  l20:mov ah,7
      int 21h
      cmp al,'q'
      je ret
      sub al,'0'
      cmp al,1
      jb l20
      cmp al,7
      ja l20
      mov [l0-1],al
      mov byte ptr [l7+2],6
      jmp $+2
      mov ah,2
      mov ch,0
      mov bh,3
   l0:mov bl,1
   l1:mov dh,0
   l3:cmp dh,ch
      je l2
      mov dl,32
      int 21h
      inc dh
      jmp l3
      ret
   l2:mov dh,bh
   l6:mov cl,12
   l5:mov dl,42
      cmp cl,bl
      ja l4
      mov dl,32
      cmp dh,1
      je l21
   l4:int 21h
      dec cl
      jnz l5
  l21:dec dh
      jnz l6
      mov dl,10
      int 21h
      mov dl,13
      int 21h
  l10:inc ch
   l9:add bl,2
   l7:cmp ch,6
      jne l1
  l13:add byte ptr [l7+2],6
  l11:dec bh
  l12:cmp bh,0
      jne l0
      xor byte ptr [l0+1],10
      xor byte ptr [l9+1],40
      xor byte ptr [l10+1],8
      xor byte ptr [l13+1],40
      sub byte ptr [l7+2],12
      mov dh,[l0-1]
      inc dh
      xor [l12+2],dh
      xor byte ptr [l11+1],8
      xor byte ptr [l1+1],1
      inc bh
      cmp byte ptr [l0+1],11
      je l0
      jmp l20

It uses lots of self-modifying code to do the triforce and its mirror, it even modifies the self-modifying code.

14
  • 10
    I could post a Base64 encoded chunk of machine code if you want ;)
    – Skizz
    Mar 9, 2010 at 14:02
  • 3
    Skizz, isn't the language called Assembly and not Assembler?
    – b.roth
    Mar 9, 2010 at 14:29
  • 3
    @Bruno: I don't think it's really a language as such. The text is just a list of machine code mnemonics that an assembler converts to machine code. Although this is often referred to as assembly language, there is no standard way of describing it, even on a single CPU it can be written in several forms. For example, Intel specifies mnemonics as op dest,src whereas a gcc listing has op src,dest. Wikipedia has a section on the terminology: en.wikipedia.org/wiki/Assembly_language#Related_terminology. It says that calling it assembler is a synonym for assembly but can lead to confusion.
    – Skizz
    Mar 9, 2010 at 15:34
  • 2
    IMHO, the key in code golf is how to complete a task with minimum code, that YOU write. So count the code you wrote, and not the compiled one. Or write already compiled code.
    – Kamarey
    Mar 11, 2010 at 23:17
  • 4
    @Kamarey: No, I think there's more to code golf than that. Language comparisons for one. Show off your skill at programming (could you show me a programmer who doesn't?). Clever implementations. etc. Would the person who wrote the python interpreter have to include that code if they posted a python version? I could create a new languauge, triforce for example, that has a single letter command which implements a solution to this problem - should I include the language code or could I claim a result of 1 char (golfscript anyone?) On this site, I think size is less important. But what do I know?
    – Skizz
    Mar 12, 2010 at 9:37
23
votes

GolfScript - 43 chars

~:!6*,{:^' '
 *'*'12*' '
  ^6%.+)*+
   -12>!^
    6/-*
     n}
     /

~:!6*,{:^' '*'*'12*' '^6%.+)*+-12>!^6/-*n}/

48 Chars for the bonus

~:!6*,.-1%+{
 :^' '*'*'12
  *' '^6%.+
   )*+-12>
    !^6/-
     *n}
      /

~:!6*,.-1%+{:^' '*'*'12*' '^6%.+)*+-12>!^6/-*n}/
2
  • @Life, not a joke. You can download golfscript from golfscript.com if you would like to try running it Jan 17, 2011 at 22:49
  • duh.. why one need to write like this, better make an ascii art Jan 18, 2011 at 9:10
17
votes

Python - 77 Chars

n=input()
for k in range(6*n):print' '*k+('*'*12+' '*(k%6*2+1))[-12:]*(n-k/6)

n=input()
for k in range(6*n):j=1+k%6*2;print' '*k+('*'*(12-j)+' '*j)*(n-k/6)

89 Chars for the bonus

n=input();R=range(6*n)
for k in R+R[::-1]:print' '*k+('*'*11+' '*11)[k%6*2:][:12]*(n-k/6)

114 Chars Version just using string replacements

u,v=' *';s=(v*11+u)*input()
while s.strip():print s;s=u+s.replace(*((v*2+u,u*3),(v*1+u*10,v*11))[' * 'in s])[:-2]

Unk Chars all in one statement, should work w/ 2.x and 3.x. The enumerate() is to allow the single input() to work for both places you need to use it.

print ('\n'.join('\n'.join(((' '*(6*n))+' '.join(('%s%s%s'%(' '*(5-x),'*'*(2*x+1),' '*(5-x)) for m in range(i + 1)))) for x in range(5,-1,-1)) for n, i in enumerate(range(int(input())-1,-1,-1))))

Yet Another Method

def f(n): print '\n'.join(' '*6*(n-r)+(' '*(5-l)+'*'*(l*2+1)+' '*(5-l)+' ')*r for r in xrange(1, n+1) for l in xrange(6))
f(input())
4
  • Unk Chars won't work in 3.x because in 3.x print is a function ;)
    – kennytm
    Mar 9, 2010 at 11:08
  • Unk Chars should be fixed now for both 2.x and 3.x Mar 9, 2010 at 13:02
  • x=xrange; ... x(6) should shorten the Yet another Method Mar 9, 2010 at 17:03
  • For someone that is trying to learn python (and programming in general), where would I be able to find an explanation of how one arrives to this answer?. I think what I'm trying to ask, is, having read the question, I currently have no clue how where I would start implementing a solution to it. Any pointers? Cheers.
    – prevailrob
    Apr 1, 2010 at 12:15
13
votes

Ruby - 74 Chars

(6*n=gets.to_i).times{|k|puts' '*k+('*'*(11-(j=k%6*2))+' '*(j+1))*(n-k/6)}
12
votes

COBOL - 385 Chars

$ cobc -free -x triforce.cob && echo 7| ./triforce

PROGRAM-ID.P.DATA DIVISION.WORKING-STORAGE SECTION.
1 N PIC 9.
1 M PIC 99.
1 value '0100***********'.
2 I PIC 99.
2 K PIC 99.
2 V PIC X(22). 
2 W PIC X(99).
PROCEDURE DIVISION.ACCEPT N
COMPUTE M=N*6
PERFORM M TIMES
DISPLAY W(1:K)NO ADVANCING
PERFORM N TIMES
DISPLAY V(I:12)NO ADVANCING
END-PERFORM
DISPLAY ''
ADD 2 TO I
IF I = 13 MOVE 1 TO I ADD -1 TO N END-IF
ADD 1 TO K
END-PERFORM.

K could be returned to outside the group level. An initial value of zero for a numeric with no VALUE clause is compiler-implementation dependent, as is an initial value of space for an alpha-numeric field (W has been cured of this, at no extra character cost). Moving K back would save two characters. -free is compiler-dependant as well, so I'm probably being over-picky.

1
  • 7
    Not enough challenges in your life, or are just masochistic?
    – Skizz
    Mar 12, 2010 at 9:40
9
votes

sed, 117 chars

s/$/76543210/
s/(.).*\1//
s/./*********** /gp
:
s/\*(\**)\*/ \1 /gp
t
:c
s/\* {11}\*/ ************/
tc
s/\*  /   /p
t

Usage: $ echo 7 | sed -rf this.sed

First attempt; improvements could probably be made...

1
  • I used a similar scheme in my 114 Char python answer :) Mar 10, 2010 at 21:40
7
votes

Ruby 1.9 - 84 characters :

v=gets.to_i
v.times{|x|6.times{|i|puts' '*6*x+(' '*i+'*'*(11-2*i)+' '*i+' ')*(v-x)}}
7
votes

Perl - 72 chars

die map$"x$_.("*"x(12-($l=1+$_%6*2)).$"x$l)x($n-int$_/6).$/,0..6*($n=<>)

78 chars

map{$l=$_%6*2;print$"x$_,("*"x(11-$l).$"x$l.$")x($n-int$_/6),$/}0..6*($n=<>)-1

87 chars

$n=<>;map{$i=int$_/6;$l=$_%6*2;print$"x$_,("*"x(11-$l).$"x$l.$")x($n-$i),$/}(0..6*$n-1)

97 chars

$n=<>;map{$i=int$_/6;$l=$_%6;print$"x(6*$i),($"x$l."*"x(11-2*$l).$"x$l.$")x($n-$i),$/}(0..6*$n-1)

108 chars

$n=<>;map{$i=int$_/6;$l=$_%6;print ""." "x(6*$i),(" "x$l."*"x(11-2*$l)." "x$l." ")x($n-$i),"\n";}(0..6*$n-1)
3
  • 97 chars, using some default perl variable values $n=<>;map{$i=int$_/6;$l=$_%6;print$"x(6*$i),($"x$l."*"x(11-2*$l).$"x$l.$")x($n-$i),$/}(0..6*$n-1)
    – Hasturkun
    Mar 9, 2010 at 10:49
  • Very nice! I'll add it to the answer. Mar 9, 2010 at 11:51
  • Using 'die' is sneaky. I forgot about that, i was trying to print the result of the map but didn't save any chars. Props! Mar 9, 2010 at 18:41
6
votes

Powershell, 78 characters

0..(6*($n=read-host)-1)|%{" "*$_+("*"*(12-($k=1+$_%6*2))+" "*$k)*(.4+$n-$_/6)}

Bonus, 92 characters

$a=0..(6*($n=read-host)-1)|%{" "*$_+("*"*(12-($k=1+$_%6*2))+" "*$k)*(.4+$n-$_/6)}
$a
$a|sort

The output is stored in an array of strings, $a, and the reverse is created by sorting the array. We could, of course, just reverse the array, but it would be more characters to type :)

5
votes

Haskell - 131 138 142 143 Chars

(⊗)=replicate
z o=[concat$(6*n+m)⊗' ':(o-n)⊗((11-m-m)⊗'*'++(1+m+m)⊗' ')|n<-[0..o-1],m<-[0..5]]
main=getLine>>=mapM_ putStrLn.z.read

This one is longer (146 148 chars) at present, but an interesting, alternate line of attack:

(⊗)=replicate
a↑b|a>b=' ';_↑_='*'
z o=[map(k↑)$concat$(6*n)⊗' ':(o-n)⊗"abcdefedcba "|n<-[0..o-1],k<-"abcdef"]
main=getLine>>=mapM_ putStrLn.z.read
5
votes

FORTRAN - 97 Chars

Got rid of the #define and saved 8 bytes thanks to implict loops!

$ f95 triforce.f95  -o triforce && echo 7 | ./triforce

READ*,N
DO K=0,N*6
M=2*MOD(K,6)
PRINT*,(' ',I=1,K),(('*',I=M,10),(' ',I=0,M),J=K/6+1,N)
ENDDO
END

125 bytes for the bonus

READ*,N
DO L=1,N*12
K=L+5
If(L>N*6)K=N*12-L+6
M=2*MOD(K,6)
PRINT"(99A)",(32,I=7,K),((42,I=M,10),(32,I=0,M),J=K/6,N)
ENDDO
END

FORTRAN - 108 Chars

#define R REPEAT
READ*,N
DO I=0,6*N
J=MOD(I,6)*2
PRINT*,R(' ',I)//R(R('*',11-J)//R(' ',J+1),N-I/6)
ENDDO
END
4
votes

JavaScript 1.8 - SpiderMonkey - 118 chars

N=readline()
function f(n,c)n>0?(c||' ')+f(n-1,c):''
for(i=0;i<N*6;i++)print(f(i)+f(N-i/6,f(11-(z=i%6*2),'*')+f(z+1)))

w/ bonus - 151 chars

N=readline()
function f(n,c)n>0?(c||' ')+f(n-1,c):''
function l(i)print(f(i)+f(N-i/6,f(11-(z=i%6*2),'*')+f(z+1)))
for(i=0;i<N*6;i++)l(i)
for(;i--;)l(i)

Usage: js thisfile.js

JavaScript - In Browser - 154 characters

N=prompt()
function f(n,c){return n>0?(c||' ')+f(n-1,c):''}
s='<pre>'
for(i=0;i<N*6;i++)s+=f(i)+f(N-i/6,f(11-(z=i%6*2),'*')+f(z+1))+'\n'
document.write(s)

The non-obfuscated version (before optimizations by gnarf):

var N = prompt();
var S = ' ';

function fill(c, n) {
    for (ret=''; n--;)
        ret += c;
    return ret;
}

var str = '<pre>';

for (i=0; i<N*6; i++) {
    str += fill(S, i);
    for (j=0; j<N-i/6; j++)
        str += fill('*', 11-i%6*2) + fill(S, i%6*2+1);
    str += '\n';
}

document.write(str);

Here's a different algorithm that uses replace() to go from one line to the next of each line of a triangle row:

161 characters

N=readline()
function f(n,c){return n>0?(c||' ')+f(n-1,c):''}l=0
for(i=N;i>0;){r=f(i--,f(11,'*')+' ');for(j=6;j--;){print(f(l++)+r)
r=r.replace(/\*\* /g,'   ')}}
2
  • Functions must have braces, at least in the Spidermonkey 'js' commandline programs packaged in the latest Fedora and Ubuntu releases. I recommend the person who removed the braces indicate what versions of Spidermonkey this works in.
    – Joey Adams
    Mar 10, 2010 at 2:58
  • 1
    JavaScript-C 1.8.0 ... Works fine from the spidermonkey-bin debian package.. Functions don't need braces or "return" for single line functions since JavaScript 1.8.0 - See stackoverflow.com/questions/2350718
    – gnarf
    Mar 10, 2010 at 9:57
4
votes

F#, 184 181 167 151 147 143 142 133 chars

let N,r=int(stdin.ReadLine()),String.replicate
for l in[0..N*6-1]do printfn"%s%s"(r l" ")(r(N-l/6)((r(11-l%6*2)"*")+(r(l%6*2+1)" ")))

Bonus, 215 212 198 166 162 158 157 148 chars

let N,r=int(stdin.ReadLine()),String.replicate
for l in[0..N*6-1]@[N*6-1..-1..0]do printfn"%s%s"(r l" ")(r(N-l/6)((r(11-l%6*2)"*")+(r(l%6*2+1)" ")))
9
  • 1
    You can improve this by 17 characters. "Int32.Parse" can be just "int". Then get rid of "open System" and just do System.Console. Get rid of needless space after "concat". Get rid of needless parens (left paren after semicolon, left paren after z=).
    – Brian
    Mar 11, 2010 at 5:55
  • 1
    Also, you can save a character with let z=(l%6)*2+1, and r(12-z)"*" and r z" ". Mar 11, 2010 at 16:03
  • Int32 cannot be replaced with just int.. That's why I need the open System.
    – m0sa
    Mar 11, 2010 at 18:40
  • @m0sa: int is a function; see: msdn.microsoft.com/en-us/library/ee370576(v=VS.100).aspx . I've updated your answer; hope you don't mind :) Mar 11, 2010 at 21:49
  • It turns out we save 4 more chracters if we remove the whole let z=... thing and just use the expression directly :) Mar 11, 2010 at 22:31
4
votes

C - 120 Chars

main(w,i,x,y){w=getchar()%8*12;for(i=0;i<w*w/2;)y=i/w,x=i++%w,putchar(x>w-2?10:x<y|w-x-1<y|(x-y)%12>=11-2*(y%6)?32:42);}

Note that this solution prints some trailing spaces (which is okay, right?). It also relies on relational operators having higher precedence than bitwise OR, saving two characters.

124 Chars

main(n,i,k){n=getchar()&7;for(k=0;k<6*n;k++,putchar(10))for(i=-k-1;++i<12*n-2*k-1;putchar(32+10*(i>=0&&(11-i%12>2*k%12))));}
2
  • Not that readability is a concern but (i>=0&&(11-i%12>2*k%12))?42:32 is equally long and also portable in those cases where true==-1... Mar 10, 2010 at 11:31
  • You can save another char by changing the second loop condition to ++i<12*n-2*k-1 Mar 10, 2010 at 11:39
3
votes

C - 177 183 Chars

#define P(I,C)for(m=0;m<I;m++)putchar(C)
main(t,c,r,o,m){scanf("%d",&t);for(c=t;c>0;c--)for(r=6;r>0;r--){P((t-c)*6+6-r,32);for(o=0;o<c;o++){P(r*2-1,42);P(13-r*2,32);}puts("");}}

C - 222 243 Chars (With Bonus Points)

#define P(I,C)for(m=0;m<I;m++)putchar(C)
main(t,c,r,o,m){scanf("%d",&t);for(c=t-1;-c<2+t;c-=1+!c)for(r=c<0?1:6;c<0?r<7:r>0;r+=c<0?1:-1){P((t-abs(c+1))*6+6-r,32);for(o=0;o<abs(c+1);o++){P(r*2-1,42);P(13-r*2,32);}puts("");}}

This is my first Code Golf submission as well!

1
  • 1
    Thanks KennyTM, I totally forgot about C and default-int parameters.
    – Adam Maras
    Mar 9, 2010 at 18:38
2
votes

Written in C

Bonus points (492 chars):

p(char *t, int c, int s){int i=0;for(;i<s;i++)printf("      ");for(i=0;i<c;i++)printf("%s",t);printf("\n");}main(int a, char **v){int i=0;int k;int c=atoi(v[1]);for(;i<c;i++){p("*********** ",c-i,i);p(" *********  ",c-i,i);p("  *******   ",c-i,i);p("   *****    ",c-i,i);p("    ***     ",c-i,i);p("     *      ",c-i,i);}for(i=0;i<c;i++){k=c-i-1;p("     *      ",1+i,k);p("    ***     ",1+i,k);p("   *****    ",1+i,k);p("  *******   ",1+i,k);p(" *********  ",1+i,k);p("*********** ",i+1,k);}}

Without bonus points (322 chars):

p(char *t, int c, int s){int i=0;for(;i<s;i++)printf("      ");for(i=0;i<c;i++)printf("%s",t);printf("\n");}main(int a, char **v){int i=0;int k;int c=atoi(v[1]);for(;i<c;i++){p("*********** ",c-i,i);p(" *********  ",c-i,i);p("  *******   ",c-i,i);p("   *****    ",c-i,i);p("    ***     ",c-i,i);p("     *      ",c-i,i);}}

First time posting, too!

2
votes

Lua, 121 chars

R,N,S=string.rep,io.read'*n',' 'for i=0,N-1 do for j=0,5 do X=R(S,j)print(R(S,6*i)..R(X..R('*',11-2*j)..X..S,N-i))end end

123

R,N,S=string.rep,io.read'*n',' 'for i=0,N-1 do for j=0,5 do print(R(S,6*i)..R(R(S,j)..R('*',11-2*j)..R(S,j)..S,N-i))end end
2
votes

PHP, 153

<?php $i=fgets(STDIN);function r($n,$c=' '){return$n>0?$c.r($n-1,$c):'';}for($l=0;$l<$i*6;){$z=$l%6*2;echo r($l).r($i-$l++/6,r(11-$z,'*').r($z+1))."\n";}

with Bonus, 210

<?php $i=fgets(STDIN);function r($n,$c=' '){return$n>0?$c.r($n-1,$c):'';}$o=array();for($l=0;$l<$i*6;){$z=$l%6*2;$o[]=r($l).r($i-$l++/6,r(11-$z,'*').r($z+1));}print join("\n",array_merge($o,array_reverse($o)));
2
  • This is 127 by my count: <?php $r='str_repeat';for($l=0;$l<($i=$argv[1]+1)*6;){$z=$l%6*2;echo$r(' ',$l).$r($r('*',11-$z).$r(' ',$z+1),$i-++$l/6)."\n";} I introduced an off by one error somewhere that I didn't bother to fix. I simply added one to $i and pre-incremented $l to compensate.
    – Matthew
    Mar 12, 2010 at 20:28
  • Or 122: <?php for($r='str_repeat';$l<($i=$argv[1]+1)*6;){$z=$l%6*2;echo$r(' ',$l).$r($r('*',11-$z).$r(' ',$z+1),$i-++$l/6)."\n";}
    – Matthew
    Mar 12, 2010 at 20:35
2
votes

dc 105 chars

123 129 132 139 141

[rdPr1-d0<P]sP?sn
0sk[1lk6%2*+sj32lkd0<Plnlk6/-si
[[*]12lj-d0<P32ljd0<Pli1-dsi0<I]dsIx
10Plk1+dskln6*>K]dsKx
2
votes

Mathematica, 46 characters

The answer prints sideways.

TableForm@{Table["*",{l,#},{l},{j,6},{2j-1}]}&
1
vote

HyperTalk - 272 chars

function triforce n
    put"******" into a
    put n*6 into h
    repeat with y=0 to h-1
        put"   " after s
        put char 1 to y of s after t
        repeat n-y div 6
            get y mod 6*2
            put char 1 to 11-it of (a&a)&&char 1 to it of s after t
        end repeat
        put return after t
    end repeat
    return t
end triforce

Indentation is neither needed nor counted (HyperCard automatically adds it).

Miscellanea:

Since there is no notion of console or way to access console arguments in HyperCard 2.2 (that I know of), a function is given instead. It can be invoked with:

on mouseUp
    ask "Triforce: "
    put triforce(it) into card field 1
end mouseUp

To use this, a card field would be created and set to a fixed-width font. Using HyperCard's answer command would display a dialog with the text, but it doesn't work because:

  • The answer dialog font (Chicago) is not fixed-width.
  • The answer command refuses to display long text (even triforce(2) is too long).
0
votes

Common Lisp, 150 characters:

(defun f(n o)(unless(= n 0)(dotimes(x 6)(format t"~v@{~a~:*~}~-1:*~v@{~?~2:*~}~%"
 o" "n"~11@: "(list(- 11(* 2 x))#\*)))(f(1- n)(+ 6 o))))
0
votes

77 char alternative python solution based on gnibbler's:

n=input()
k=0
exec"print' '*k+('*'*12+' '*(k%6*2+1))[-12:]*(n-k/6);k+=1;"*6*n

Amazingly the bonus came out exactly the same also (101 chars, oh well)

n=input()
l=1
k=0
s="print' '*k+('*'*12+' '*(k%6*2+1))[-12:]*(n-k/6);k+=l;"*6*n
exec s+'l=-1;k-=1;'+s

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