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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.