Created
April 15, 2010 18:09
-
-
Save kamipo/367427 to your computer and use it in GitHub Desktop.
hanoi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
sub hanoi { | |
my ($from, $to, $spare, $num) = @_; | |
my $hanoi = $num > 1 ? \&hanoi : sub {}; | |
$hanoi->($from, $spare, $to, $num - 1); | |
move($from, $to); | |
$hanoi->($spare, $to, $from, $num - 1); | |
} | |
sub move { unshift @{$_[1]}, shift @{$_[0]} } | |
my $a = [qw/1 2 3/]; | |
my $b = []; | |
my $c = []; | |
use Data::Dump qw/dump/; | |
sub yeah { print dump({a => $a, b => $b, c => $c}), $/ } | |
yeah; | |
hanoi $a, $c, $b, 0+@$a; | |
yeah; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment