Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
And here is my solution.
#! /usr/bin/perl use strict; my ($peg_count, $disk_count) = @ARGV; my $peg = 'A'; my @list; push @list, $peg++ for 1..$peg_count; solve(\@list, [reverse 1..$disk_count]); exit(0); # Per peg and number of disks, how many moves to solve the puzzle. my %cost; # Per number of pegs and disks, how many will not go on the last peg. my %use_some_for; sub strategize { my ($pegs, $disks) = @_; if (defined($use_some_for{$pegs}[$disks])) { # Already solved } elsif ($disks < 2) { $cost{$pegs}[0] = 0; $use_some_for{$pegs}[0] = 0; $cost{$pegs}[1] = 1; $use_some_for{$pegs}[1] = 1; } elsif (3 == $pegs) { strategize($pegs, $disks - 1); $cost{$pegs}[$disks] = 1 + 2*$cost{$pegs}[$disks - 1]; $use_some_for{$pegs}[$disks] = 1; } else { strategize($pegs, $disks - 1); my $lower_some = $use_some_for{$pegs}[$disks - 1]; strategize($pegs - 1, $lower_some + 1); my $lower_cost = 2*$cost{$pegs}[$disks - $lower_some] + $cost{$pegs - 1}[$lower_some]; my $upper_cost = 2*$cost{$pegs}[$disks - $lower_some - 1] + $cost{$pegs - 1}[$lower_some + 1]; if ($lower_cost < $upper_cost) { $cost{$pegs}[$disks] = $lower_cost; $use_some_for{$pegs}[$disks] = $lower_some; } else { $cost{$pegs}[$disks] = $upper_cost; $use_some_for{$pegs}[$disks] = $lower_some + 1; } } return $use_some_for{$pegs}[$disks]; } sub solve { my ($peg_list, $disk_list) = @_; if (0 == @$disk_list) { return; # No steps } elsif (1 == @$disk_list) { print "$disk_list->[0]: $peg_list->[0] -> $peg_list->[1]\n"; } else { my $pegs = @$peg_list; my $last_disk = $#$disk_list; my $cutoff = strategize($pegs, 1 + $last_disk); die "No cutoff found for $pegs, $last_disk\n" unless defined($cutoff); my $from = shift @$peg_list; my $to = shift @$peg_list; my $hold = pop @$peg_list; solve([$from, $hold, $to, @$peg_list], [@$disk_list[$cutoff..$last +_disk]]); solve([$from, $to, @$peg_list], [@$disk_list[0..($cutoff-1)]]); solve([$hold, $to, $from, @$peg_list], [@$disk_list[$cutoff..$last +_disk]]); } }
I think that blokhead was headed in this direction and would have gotten there in time. I'm curious about tye's reasoning, and suspect that he was close to the same path that I followed. Here are details.

The key is that in the recursion you only need to worry about how many you're going to pile up on the last peg. If you do that, then you don't need to have a simple rule for the optimal strategy - you can just calculate it recursively. That is what my strategize function does. And it memoizes the result for performance reasons.

Of course this optimizer only optimizes you among strategies where you recursively take a set of disks and put them on another peg, move the remaining disks to the final peg, then move the first set of disks to the final peg. There are many other possible strategies (for instance meaningless move-order changes to this one) that can be tried. I have no idea how one would go around proving that no other kind of strategy can beat this one. Judging from this mathworld article, apparently nobody else has figured that out either. (Incidentally that article gives an explicit formula for the conjectured sequence for 4 pegs. It is sub-exponential, but still grows faster than any polynomial.)

At 10 disks I do only marginally better, solving the 4 peg case in 49 moves rather than 57, and the 5 peg case in 31 rather than 35 moves. (Incidentally I'm curious howtye figured out that sequence which showed that 31 is best.) For all other numbers of pegs we're tied.

At 20 disks the improvement of being uneven is more dramatic. With 4 pegs, 289 vs 1137 moves. With 5 pegs 111 vs 199 moves. With 6 pegs 98 vs 97 moves. With 7 pegs 67 vs 75 moves. With 8 pegs 65 vs 69 moves. We meet again at 9 pegs with 63 moves.


In reply to Re: Hanoi Challenge by tilly
in thread Hanoi Challenge by tilly

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-18 10:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found