Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Golf: Triforce

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.


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

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

Ruby - 74 Chars

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

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.


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