aboutsummaryrefslogtreecommitdiff
path: root/challenge-219/matthias-muth/perl/ch-2.pl
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-219/matthias-muth/perl/ch-2.pl')
-rwxr-xr-xchallenge-219/matthias-muth/perl/ch-2.pl36
1 files changed, 36 insertions, 0 deletions
diff --git a/challenge-219/matthias-muth/perl/ch-2.pl b/challenge-219/matthias-muth/perl/ch-2.pl
new file mode 100755
index 0000000000..7b3234b92d
--- /dev/null
+++ b/challenge-219/matthias-muth/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+#
+# The Weekly Challenge - Perl & Raku
+# (https://theweeklychallenge.org)
+#
+# Challenge 219 Task 2: Travel Expenditure
+#
+# Perl solution by Matthias Muth.
+#
+
+use strict;
+use warnings;
+use feature 'say';
+
+use lib '.';
+use TestExtractor;
+
+use List::Util qw( min );
+
+my @durations = ( 1, 7, 30 );
+
+sub travel_expenditure {
+ my ( $costs, $days ) = @_;
+ return 0
+ if @$days == 0;
+ return min(
+ map {
+ my $duration = $durations[$_];
+ $costs->[$_]
+ + travel_expenditure( $costs,
+ [ grep $_ >= $days->[0] + $duration, @$days ] );
+ } 0..$#{$costs}
+ );
+}
+
+run_tests;