From 0c539efcc441d3c506aa785a423f941fbd9a7e66 Mon Sep 17 00:00:00 2001 From: Lance Wicks Date: Sat, 26 Jun 2021 11:23:35 +0100 Subject: Add a script to be run on CLI, repeats 99999 times so a relatively short path is found --- challenge-118/lance-wicks/perl/ch-2.pl | 29 ++++++++++++++++++++++++++++ challenge-118/lance-wicks/perl/lib/Knight.pm | 5 ++--- 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 challenge-118/lance-wicks/perl/ch-2.pl 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; -- cgit