Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Triforce Programming Challenge

Credits go to axilmar, Bruce Perry, Dustin Howett, kazzmir, machine, Martin Kalbfuß, Matthew Leverton, Peter Wang, and Xuzzers for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
Triforce Programming Challenge
Arthur Kalliokoski
Second in Command
February 2005
avatar

Quote:

I seen the original post

I first saw the original post?

They all watch too much MSNBC... they get ideas.

Kibiz0r
Member #6,203
September 2005
avatar

Concurrent version lolol:

#SelectExpand
1using System; 2using System.Linq; 3 4namespace ConsoleApplication1 5{ 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 var iterations = Int32.Parse(Console.ReadLine()); 11 var reverse = Boolean.Parse(Console.ReadLine()); 12 var triforce = from i in ParallelEnumerable.Range(0, iterations) 13 from j in ParallelEnumerable.Range(0, 6) 14 select " ".Repeat(6 * i) + (" ".Repeat(j) + "*".Repeat(11 - j * 2) + " ".Repeat(j + 1)).Repeat(iterations - i); 15 Console.WriteLine(reverse ? String.Join(Environment.NewLine, triforce.Concat(triforce.Reverse())) : String.Join(Environment.NewLine, triforce)); 16 } 17 } 18}

1000 iterations (and reverse)
Single-threaded: 1.175s
Multi-threaded: 0.669s

bamccaig
Member #7,536
July 2006
avatar

Kibiz0r said:
var iterations = Int32.Parse(Console.ReadLine());
var reverse = Boolean.Parse(Console.ReadLine());

That's just ugly. :-/ It would be easier to use args. >:( Both Parse methods are going to throw if the line isn't parse-able anyway so if you were going for compact you could just ignore an args.Length check and get similar behavior for incorrect usage.

Bruce Perry
Member #270
April 2000

triforce :- triforce(3).
triforce(N) :- triforce(N,6).
triforce(N,Size) :- triforce(N,Size,true).
triforce(N,Size,Reverse) :- triforce(N,Size,Reverse,0).
triforce(N,Size,Reverse,Indent) :- N>0, triforce(N,Size,Reverse,Indent,0).
triforce(0,_Size,_Reverse,_Indent).
triforce(N,Size,Reverse,Indent,Row) :-
  Row<Size, Off is 1+Row*2, On is Size*2-Off, Indent2 is Indent+1, Row2 is Row+1,
  tfScan(N,Indent,On,Off), triforce(N,Size,Reverse,Indent2,Row2), (Reverse, tfScan(N,Indent,On,Off); true).
triforce(N,Size,Reverse,Indent,Size) :- N2 is N-1, triforce(N2,Size,Reverse,Indent).
tfScan(N,Indent,On,Off) :- N>0, tfRun(' ',Indent), tfRun('*',On), N2 is N-1, tfScan(N2,Off,On,Off).
tfScan(0,_Indent,_On,_Off) :- nl.
tfRun(C,M) :- M>0, write(C), M2 is M-1, tfRun(C,M2).
tfRun(_C,0).

Can be invoked with a query such as

?- triforce.
?- triforce(3).
?- triforce(3,6).
?- triforce(3,6,true).

where the 3 is the number of triangles, the 6 is the size of each one, and the Boolean is whether to print the reverse Triforce.

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Neil Black
Member #7,867
October 2006
avatar

X-G said:

All I keep thinking is newfags can't triforce

  ▲
▲ ▲

Bruce Perry
Member #270
April 2000

My program briefly output

▲ ▲
at one stage during its development. Much hilarity ensued.

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Kibiz0r
Member #6,203
September 2005
avatar

X-G said:

All I keep thinking is newfags can't triforce

My Facebook after posting my solution said:

Newfags can't from i in Enumerable.Range(0, iterations) from j in Enumerable.Range(0, 6) select repeat(" ", 6 * i) + repeat(repeat(" ", j) + repeat("*", 11 - j * 2) + repeat(" ", j + 1), iterations - i);

Neil Black
Member #7,867
October 2006
avatar

Kibiz0r said:

Newfags can't from i in Enumerable.Range(0, iterations) from j in Enumerable.Range(0, 6) select repeat(" ", 6 * i) + repeat(repeat(" ", j) + repeat("*", 11 - j * 2) + repeat(" ", j + 1), iterations - i);

You win. I'm not sure what you win, but you definitely win.

BAF
Member #2,981
December 2002
avatar

Did you seriously write that in Prolog? :o

Bruce Perry
Member #270
April 2000

Yes :)

And what's more, a female friend got me into it :P

--
Bruce "entheh" Perry [ Web site | DUMB | Set Up Us The Bomb !!! | Balls ]
Programming should be fun. That's why I hate C and C++.
The brxybrytl has you.

Arthur Kalliokoski
Second in Command
February 2005
avatar

They all watch too much MSNBC... they get ideas.

Kibiz0r
Member #6,203
September 2005
avatar

Oh wow.

bamccaig
Member #7,536
July 2006
avatar

Martin Kalbfuß
Member #9,131
October 2007
avatar

A bit late, but the compiler had a bug, which is fixed now.

The Modula-2 version:

#SelectExpand
1MODULE triforce; 2 3FROM STextIO IMPORT WriteChar, WriteString, WriteLn; 4FROM SWholeIO IMPORT ReadCard; 5 6VAR n : CARDINAL; 7 8PROCEDURE DrawTriforce(cur, max : CARDINAL); 9VAR i1, i2, i3 : CARDINAL; 10BEGIN 11 IF cur > max THEN RETURN END; 12 FOR i1 := 0 TO 5 DO 13 FOR i2 := 0 TO cur * 6 DO WriteChar(' ') END; 14 FOR i2 := 0 TO max - cur DO 15 FOR i3 := 1 TO i1 DO WriteChar(' ') END; 16 FOR i3 := 1 TO (11 - i1 * 2) DO WriteChar('*') END; 17 FOR i3 := 0 TO i1 DO WriteChar(' ') END; 18 END; 19 WriteLn(); 20 END; 21 DrawTriforce(cur + 1, max); 22END DrawTriforce; 23 24BEGIN 25 WriteString('Amount: '); 26 ReadCard(n); 27 DrawTriforce(0, n - 1); 28END triforce.

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

machine
Member #11,569
December 2009

Just saw this challenge, not planning on doing it in full yet, but the simplest way I find to generate a triangle is to think of it as a square, and then increasingly "sculpt" out the edges.

int end = 11; int begin = 0;
for(int i = 0; i < 6; i++) { /*six rows*/
for(int k = 0; k < begin; k++) { std::cout << " "; }
for(int j = 0; j < end; j++) { std::cout << "*"; } std::cout << "\n"; end -= 2; begin++;
}

Kibiz0r
Member #6,203
September 2005
avatar

Well, this is pretty much done. Good job, everybody! ;D

 1   2 


Go to: