aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Pankoff <ccntrq@screenri.de>2022-02-18 14:47:48 +0100
committerAlexander Pankoff <ccntrq@screenri.de>2022-02-18 15:24:08 +0100
commit3f44c11807d4a55fbe1a3cbb1ed77361b0ed192a (patch)
tree22e4292e13ba2b19931995e801831b1cd520dfab
parentcfd4264b65cc7b38c99b0f28f692cd4e359b7722 (diff)
downloadperlweeklychallenge-club-3f44c11807d4a55fbe1a3cbb1ed77361b0ed192a.tar.gz
perlweeklychallenge-club-3f44c11807d4a55fbe1a3cbb1ed77361b0ed192a.tar.bz2
perlweeklychallenge-club-3f44c11807d4a55fbe1a3cbb1ed77361b0ed192a.zip
Minor improvements to challenge 151 task 2
-rwxr-xr-xchallenge-151/alexander-pankoff/perl/ch-2.pl25
1 files changed, 9 insertions, 16 deletions
diff --git a/challenge-151/alexander-pankoff/perl/ch-2.pl b/challenge-151/alexander-pankoff/perl/ch-2.pl
index 8fb1634a84..e86839a890 100755
--- a/challenge-151/alexander-pankoff/perl/ch-2.pl
+++ b/challenge-151/alexander-pankoff/perl/ch-2.pl
@@ -29,31 +29,24 @@ no warnings qw'experimental::signatures';
use List::Util qw(all reduce sum0);
-use Data::Dumper;
-
run() unless caller();
sub run() {
my @valuables = @ARGV;
die "Invalid input\n" unless all { m/^-?\d+$/ } @valuables;
- rob_house(@valuables);
-
+ say rob_house(@valuables);
}
sub rob_house (@valuables) {
- my @tours = plan_tour($#valuables);
+ my @tours = plan_tours($#valuables);
my $best_tour = reduce sub {
my @tour = @$b;
my $tour_value = sum0 map { $valuables[$_] } @tour;
- if ( $tour_value > $a->{value} ) {
- return {
- value => $tour_value,
- tour => [@tour],
- };
- }
- if ( $tour_value == $a->{value} && @tour < @{ $a->{tour} } ) {
+ if ( $tour_value > $a->{value}
+ || $tour_value == $a->{value} && @tour < @{ $a->{tour} } )
+ {
return {
value => $tour_value,
tour => [@tour],
@@ -64,14 +57,14 @@ sub rob_house (@valuables) {
}, { value => 0, tour => [] }, @tours;
- print Dumper $best_tour;
+ return $best_tour->{value};
}
-sub plan_tour ( $max, $cur = 0 ) {
+sub plan_tours ( $max, $cur = 0 ) {
return [] if $cur > $max;
my @paths = (
- ( map { [ $cur, @$_ ] } plan_tour( $max, $cur + 2 ) ),
- plan_tour( $max, $cur + 1 )
+ ( map { [ $cur, @$_ ] } plan_tours( $max, $cur + 2 ) ),
+ plan_tours( $max, $cur + 1 )
);
return @paths;