Code Golf: Triforce

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

Problem

Suppose a user enters numerical input in the range of 1 to 7. The input should be cleared from the console, arguments are less desirable.

When input 1, type the following:

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

Values ​​greater than one should generate multiples of the pattern ending one higher, but add symmetrically. For example, 3 should print the following:

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

Bonus points if you also print reverse.

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

Can we try to keep one answer in one language, that we will improve everything?

+48
language-agnostic code-golf rosetta-stone
Mar 09 '10 at 5:02
source share
23 answers

Assembler assembled 165 bytes

Assembly instructions

  • Download A86 from here.
  • Add A86 executable link to DOS search path
  • Paste the code below into a text file (example: triforce.asm )
  • Assembler call: a86 triforce.asm
  • This will create a .COM file called triforce.com
  • Enter triforce to run

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

Build with A86 and request the WinXP DOS window to run the .COM file it created. Press "q" to exit, keys 1-7 to output 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 a lot of self-modifying codes to make triforce and its mirror, it even modifies the self-modifying code.

+39
Mar 09 '10 at 11:17
source share
— -

GolfScript - 43 characters

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

48 bonus balls

 ~:!6*,.-1%+{ :^' '*'*'12 *' '^6%.+ )*+-12> !^6/- *n} / ~:!6*,.-1%+{:^' '*'*'12*' '^6%.+)*+-12>!^6/-*n}/ 
+23
Mar 09 '10 at 9:33
source share

Python - 77 Chars

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

89 Chars for a bonus

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

114 Chars Version using string replacement

 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 expression, w / 2.x and 3.x should work. The enumerate () function allows a single input () to work for both places that you need to use.

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

Another method

 def f(n): print '\n'.join(' '*6*(nr)+(' '*(5-l)+'*'*(l*2+1)+' '*(5-l)+' ')*r for r in xrange(1, n+1) for l in xrange(6)) f(input()) 
+17
Mar 09 '10 at 6:05
source share

Ruby - 74 Chars

 (6*n=gets.to_i).times{|k|puts' '*k+('*'*(11-(j=k%6*2))+' '*(j+1))*(nk/6)} 
+13
Mar 09 '10 at 11:18
source share

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 can be returned outside the group level. The initial zero value for numeric without the VALUE clause depends on the compiler implementation, since this is the initial space value for the alpha-numeric field (W was cured of this without the extra cost of a character). Moving K back would save two characters. -free also depends on the compiler, so I'm probably too picky.

+12
Mar 10
source share

sed, 117 characters

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

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

First try; perhaps improvements ...

+9
Mar 09 '10 at 14:05
source share

Ruby 1.9 - 84 characters:

 v=gets.to_i v.times{|x|6.times{|i|puts' '*6*x+(' '*i+'*'*(11-2*i)+' '*i+' ')*(vx)}} 
+7
Mar 09 '10 at 6:51
source share

Perl - 72 characters

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

78 characters

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

87 characters

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

97 characters

 $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 characters

 $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) 
+7
Mar 09 '10 at 8:51
source share

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 opposite is created by sorting the array. Of course, we could just change the array, but there would be more characters to print :)

+6
Mar 09 '10 at 22:11
source share

Haskell - 131 138 142 143 Chars

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



It is currently longer (146,148), but an interesting alternative line of attack:

 (⊗)=replicate a↑b|a>b=' ';__='*' zo=[map(k↑)$concat$(6*n)⊗' ':(on)⊗"abcdefedcba "|n<-[0..o-1],k<-"abcdef"] main=getLine>>=mapM_ putStrLn.z.read 
+5
Mar 09 '10 at 6:45
source share

FORTRAN - 97 Chars

Got an exemption from #define and saved 8 bytes thanks to implicit 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 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),NI/6) ENDDO END 
+5
Mar 09 '10 at 10:19
source share

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((tc)*6+6-r,32);for(o=0;o<c;o++){P(r*2-1,42);P(13-r*2,32);}puts("");}} 

C - 222 243 Balls (with bonuses)

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

+4
Mar 09 '10 at 7:27
source share

JavaScript 1.8 - SpiderMonkey - 118 characters

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

w / bonus - 151 characters

 N=readline() function f(n,c)n>0?(c||' ')+f(n-1,c):'' function l(i)print(f(i)+f(Ni/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 the 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(Ni/6,f(11-(z=i%6*2),'*')+f(z+1))+'\n' document.write(s) 

Non-confusing version (before gnarf optimization):

 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<Ni/6; j++) str += fill('*', 11-i%6*2) + fill(S, i%6*2+1); str += '\n'; } document.write(str); 

Here, another algorithm is used that uses replace () to go from one line to the next each line of the line of the triangle:

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,' ')}} 
+4
Mar 09 2018-10-10T00:
source share

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

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

Bonus 215 212 198 166 162 158 157 148 characters

 let N,r=int(stdin.ReadLine()),String.replicate for l in[0..N*6-1]@[N*6-1..-1..0]do printfn"%s%s"(rl" ")(r(Nl/6)((r(11-l%6*2)"*")+(r(l%6*2+1)" "))) 
+4
Mar 09 '10 at 23:28
source share

C - 120 characters

 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|wx-1<y|(xy)%12>=11-2*(y%6)?32:42);} 

Note that this solution prints some trailing spaces (which is good, right?). It also relies on relational operators with a higher priority than bitwise ORs, storing 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))));} 
+4
Mar 10
source share

dc 105 characters

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
Mar 09 '10 at 8:45
source share

Written in C

Reward Points (492 characters):

 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("*********** ",ci,i);p(" ********* ",ci,i);p(" ******* ",ci,i);p(" ***** ",ci,i);p(" *** ",ci,i);p(" * ",ci,i);}for(i=0;i<c;i++){k=ci-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);}} 

No bonus points (322 characters):

 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("*********** ",ci,i);p(" ********* ",ci,i);p(" ******* ",ci,i);p(" ***** ",ci,i);p(" *** ",ci,i);p(" * ",ci,i);}} 

First post too!

+2
Mar 09 '10 at 8:51
source share

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,Ni))end end 

123 Strike>

 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,Ni))end end 
+2
Mar 09 '10 at 14:27
source share

PHP 153

 <?php $i=fgets(STDIN);function r($n,$c=' '){return$n>0?$cr($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?$cr($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
Mar 10 '10 at 1:05
source share

Mathematica, 46 characters

The answer is printed to the side.

 TableForm@{Table["*",{l,#},{l},{j,6},{2j-1}]}& 
+2
Mar 22 2018-11-22T00:
source share

HyperTalk - 272 characters

 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 ny 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 not needed or counted (HyperCard automatically adds it).

Miscellaneous:

Since there is no console concept or way to access console arguments in HyperCard 2.2 (what I know), a function is provided instead. It can be called using:

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

To use this, a map field will be created and a fixed-width font will be set. Using the HyperCard response command displays a dialog with text, but this does not work because:

  • The response dialog font (Chicago) does not have a fixed width.
  • The response command refuses to display long text (even triforce (2) is too long).
+1
Mar 11 '10 at 6:03
source share

Generic Lisp, 150 characters:

  (defun f (no) (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
Mar 10 '10 at 1:20
source share

77 char gnibbler's alternative python solution:

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

Surprisingly, the bonus came out exactly the same (101 characters, well, good)

 n=input() l=1 k=0 s="print' '*k+('*'*12+' '*(k%6*2+1))[-12:]*(nk/6);k+=l;"*6*n exec s+'l=-1;k-=1;'+s 
0
Mar 10 '10 at 9:43
source share



All Articles