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

Re: [MacPerl] The Powers of Annoy



Lieber Detlef,
(Dear Detlef,)

Merci beaucoup!
(Çok teSekkür ederim)

"The Towers of *Hanoi*", klar, war nur ein Wortspiel, weil das Ding ja nun
inzwischen fast jeder Programmierer kennt. Danke für Deine Tipps, schon
umgesetzt, und neu "postiert". (The "Towers of Hanoi", sure, ¹twas just a
pun, as nearly every programmer knows the exercise by now. Thanks a lot for
your clues, they have been incorporated already, and the revised code should
be on your screen right now.)


#
# A non-recursive solution to the "Towers of Hanoi"
# by miku@tgsh.de, r10 Jan 2000
# (thanks to Detlef Lindenthal for style advice)
#
#!/usr/local/bin/perl
#

##########################################################################
##
## Init main vars and output "minimalist" header
##
##########################################################################

$full = "54321";                     # disks on first stick & end criterion
$" = ", ";                           # delimiter to separate sticks output
$fl= length($full); $end = 2**$fl-1; # determine maximal number of transfers
$small = 0;                          # no. stick holding the smallest disk
 
@sticks = ($full, "", "");           # init all three sticks
print "\n\nThe Towers of Hanoi\n\n";
print "Initial state: @sticks\n";
 

############################################################################
#
# The idea of the algorithm:
#   1) Move the smallest disk in a clock-wise fashion
#   2) Move another disk
#   3) Run through steps 1) to 3) until the whole tower is transferred
#                  
############################################################################

until ($nr_moves >= $end) {            # Already done with it all?
   chop(@sticks[$small]);              # No, so remove the smallest disk...
   $stick2 = $small;                   # ...but remember its position.
   $small=(($small+1)%3);              # Clockwise increment its position
   $sticks[$small] .= "1";             # Now move it on...

   $nr_moves++;                        # ... and update the move counter.
   print "Move #$nr_moves: @sticks\n"; # Output current overall stick state
    
   unless ($nr_moves >= $end) {        # Are there still disks to move?
   $len2 = length(@sticks[$stick2]);   # Yes. Now: what is on last stick?
   $char2 = substr(@sticks[$stick2],$len2-1,1);  # Size of uppermost disk

   $stick3 = (3-($small+$stick2));     # Which is the last untouched stick?
   $len3 = length(@sticks[$stick3]);   # How many disks reside there?
   $char3 = substr(@sticks[$stick3],$len3-1,1);  # Size of its topmost disk


############################################################################
#                  
# The next four lines determine which disk to move to what stick.
#   There is exactly one legal move left in this round, so the idea is:
#   If one of the two possible sticks is void of disks, fill it up.
#   If none of the sticks is currently empty,
#   which disk (of the two candidates) is smallest?
#   Then: move smallest possible disk to the remaining empty stick.
#                  
############################################################################

   if ($char3 eq "") {chop(@sticks[$stick2]); @sticks[$stick3] .= $char2}
   elsif ($char2 eq "") {chop(@sticks[$stick3]); @sticks[$stick2] .= $char3}
   elsif ($char3 lt $char2) {chop(@sticks[$stick3]); @sticks[$stick2] .=
$char3}
   else {chop(@sticks[$stick2]); @sticks[$stick3] .= $char2}

   $nr_moves++;
   print "Move #$nr_moves: @sticks\n";  # ok, output current state again
}}





Das war's denn auch schon (That's it.)
Danke nochmals für die Tipps. (Again, thanks for the tips).

Frohes Schaffen!

Michael
(Michael:)




P.S. Kennst Du eine rekursive Implementierung der Tuerme in PERL?
     (Do you know a PERL Hanoi implementation that makes use of recursion?)


# ===== Want to unsubscribe from this list?
# ===== Send mail with body "unsubscribe" to macperl-request@macperl.org