[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Towers of Hanoi



On Thu, Sep 14, 2000 at 11:57:04PM +0300, Ilmari Karonen wrote:
> 
> [Towers of Hanoi]
> 
> Even the toy version can be quite frustrating unless you know the optimal
> sequence, since it requires a sort of recursive "to go right, first go as
> far left as you can" approach humans are notoriously bad at.


Everyone thinks Towers of Hanoi needs recursion to solve. Nothing is further
from the truth. Regexes are the way to go for Towers of Hanoi! Here's a
two line solution (which requires a pre-5.6.0 Perl):

%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


(You need to give the number of disks as argument to the program).



Abigail

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe