Everyone thinks that Towers of Hanoi is best done with a recursive algorithm. But as we all know, function calling is slow, Perl warns if you go into recursion to often, and frankly, recursive programs are boring and just way to long.
Towers of Hanoi is best solved with a regex, and just two lines of code. Unfortunally, for some odd reason Perl was changed, and the following code does not run in Perl 5.6.0.
%0=map{reverse+chop,$_}ABC,ACB,BAC,BCA,CAB,CBA;$_=shift().AC;1while+s/ +(\d+)((.) (.))/($0=$1-1)?"$0$3$0{$2}1$2$0$0{$2}$4":"$3 => $4\n"/xeg;print#Towers + of Hanoi
-- Abigail
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Towers of Hanoi
by LeGo (Chaplain) on Jul 24, 2001 at 01:26 UTC |