Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Towers of Hanoi

by Anonymous Monk
on Jul 12, 2000 at 06:48 UTC ( [id://22144]=obfuscated: print w/replies, xml ) Need Help??

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
    I am curious as to if someone can help guide me in how to solve this or show me how to solve this. I am looking to be able to solve it with any number of variables. For example

    perl hanoi.pl 4

    If no variable was passed I would like for it to default to just 3. I am looking to just impress my friends. I am taking psy200. I am not sure how to begin to look at solving this. If the above was typed in at the command line I would like to see the following result.

    A-B
    A-C
    B-C
    A-B
    C-A
    C-B
    A-B
    A-C
    B-C
    B-A
    C-A
    B-C
    A-B
    A-C
    B-C
    DONE!!!!! Finished in 15 moves!!!!

    This is more so illustrated at this site and also solved for 3-7. I am looking to solve for any number (or as many as possible). Any help in this would be awesome, as I am sure this will be a fun project.

    Thanks,

    LeGo

    UPDATE: I found the solution above did work for me. It just needed the arguement passed in on command line. The answer was posted in more detail here. It would still be nice to be able to see how many moves it took...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://22144]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-19 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found