aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-118/lance-wicks/perl/ch-2.pl29
-rw-r--r--challenge-118/lance-wicks/perl/lib/Knight.pm5
2 files changed, 31 insertions, 3 deletions
diff --git a/challenge-118/lance-wicks/perl/ch-2.pl b/challenge-118/lance-wicks/perl/ch-2.pl
new file mode 100644
index 0000000000..9dbe2d3d0d
--- /dev/null
+++ b/challenge-118/lance-wicks/perl/ch-2.pl
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+use lib './lib';
+use Knight;
+
+$|=1;
+
+my $treasures
+ = [ [ 0, 1 ], [ 1, 0 ], [ 1, 1 ], [ 2, 1 ], [ 3, 2 ], [ 5, 4 ] ];
+
+my $least = 9999;
+my $path;
+
+for ( 1 .. 99999 ) {
+ my $k = Knight->new( treasures => $treasures );
+ my $res = $k->go;
+ if ( $res->{moves} < $least ) {
+ $least = $res->{moves};
+ $path = $res->{path};
+ print "$least\n";
+ }
+}
+
+print "\n\nPath: ";
+for (@$path) {
+ print "($_->[0],$_->[1]) ";
+}
+print "\nLeast moves: $least\n";
diff --git a/challenge-118/lance-wicks/perl/lib/Knight.pm b/challenge-118/lance-wicks/perl/lib/Knight.pm
index 9a185c83fe..afb6affec3 100644
--- a/challenge-118/lance-wicks/perl/lib/Knight.pm
+++ b/challenge-118/lance-wicks/perl/lib/Knight.pm
@@ -33,8 +33,8 @@ sub go {
return {
treasures => $self->collected_treasures,
- path => @path,
- moves => 0 + @path
+ path => \@path,
+ moves => @path -1, # sub 1 as path includes starting position.
};
}
@@ -55,7 +55,6 @@ sub move {
while (1) {
my $direction = int rand(8);
- #warn "**** $row, $col -> $direction";
$new_row = 0;
$new_col = 0;