Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Hanoi Challenge

by tilly (Archbishop)
on Oct 23, 2004 at 17:04 UTC ( #401859=note: print w/replies, xml ) Need Help??


in reply to Hanoi Challenge

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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2022-01-29 02:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?