Skip to content

Instantly share code, notes, and snippets.

@kamipo
Created April 15, 2010 18:09
Show Gist options
  • Save kamipo/367427 to your computer and use it in GitHub Desktop.
Save kamipo/367427 to your computer and use it in GitHub Desktop.
hanoi
#!/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