aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-31 22:28:07 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-05-31 22:28:07 +0100
commitb12efab021fa5d0bf09f3ef0dc1bee34d63586f2 (patch)
tree0a9feee897efdfb40f262143c649f0b76482d689
parent0d858f48d5e666e96ce56f1a723f9b11b6aea72a (diff)
parent749efd944ae6b383716268b4e17f461124de89e8 (diff)
downloadperlweeklychallenge-club-b12efab021fa5d0bf09f3ef0dc1bee34d63586f2.tar.gz
perlweeklychallenge-club-b12efab021fa5d0bf09f3ef0dc1bee34d63586f2.tar.bz2
perlweeklychallenge-club-b12efab021fa5d0bf09f3ef0dc1bee34d63586f2.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-219/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-219/arne-sommer/raku/ch-1.raku5
-rwxr-xr-xchallenge-219/arne-sommer/raku/ch-2.raku77
-rwxr-xr-xchallenge-219/arne-sommer/raku/sorted-squares5
-rwxr-xr-xchallenge-219/arne-sommer/raku/travel-expenditure77
5 files changed, 165 insertions, 0 deletions
diff --git a/challenge-219/arne-sommer/blog.txt b/challenge-219/arne-sommer/blog.txt
new file mode 100644
index 0000000000..30fec0129b
--- /dev/null
+++ b/challenge-219/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/sorted-expenditure.html
diff --git a/challenge-219/arne-sommer/raku/ch-1.raku b/challenge-219/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..712abaf756
--- /dev/null
+++ b/challenge-219/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,5 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@list where @list.elems >= 1 && all(@list) ~~ Int);
+
+say '(' ~ @list.map( * ** 2 ).sort.join(", ") ~ ')';
diff --git a/challenge-219/arne-sommer/raku/ch-2.raku b/challenge-219/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..4f30e24971
--- /dev/null
+++ b/challenge-219/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,77 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($costs, $days, :v(:$verbose));
+
+my @costs = $costs.words>>.Numeric;
+
+die "Specify three costs" unless @costs.elems == 3;
+die "Costs must be numeric" unless all(@costs) ~~ Numeric;
+
+my %costs = ( 1 => @costs[0], 7 => @costs[1], 30 => @costs[2] );
+
+my @days = $days.words>>.UInt;
+
+die "The days must be positive integers (1..366)" unless all(@days) ~~ UInt && 1 <= all(@days) <= 366;
+
+my @res = gather
+{
+ recurse(log => (), total-cost => 0, todo => @days.sort);
+}
+
+my $lowest = Inf;
+
+for @res -> $row
+{
+ print ": Cost: $row{'cost'}" if $verbose;
+
+ if $row{'cost'} < $lowest
+ {
+ $lowest = $row{'cost'};
+ say " - new lowest" if $verbose;
+ }
+ elsif $verbose
+ {
+ say "";
+ }
+
+ if $verbose
+ {
+ for @($row{'log'}) -> $log
+ {
+ say ": - $log";
+ }
+ say "";
+ }
+}
+
+say $lowest;
+
+sub recurse (:@log, :$total-cost, :@todo is copy)
+{
+ my $today = @todo.shift;
+
+ for 1,7,30 -> $days
+ {
+ my $paid = $total-cost + %costs{$days};
+ my $paid-to = $today + $days;
+ my @covered = ($today,);
+ my @new-todo = @todo.clone;
+ my @new-log = @log.clone;
+
+ while @new-todo.elems && @new-todo[0] < $paid-to
+ {
+ @covered.push: @new-todo.shift;
+ }
+
+ @new-log.push: "At day $today pay for $days day(s) ({ @covered.join(",") })";
+
+ if @new-todo.elems
+ {
+ recurse(log => @new-log, total-cost => $paid, todo => @new-todo)
+ }
+ else
+ {
+ take { cost => $paid, log => @new-log };
+ }
+ }
+}
diff --git a/challenge-219/arne-sommer/raku/sorted-squares b/challenge-219/arne-sommer/raku/sorted-squares
new file mode 100755
index 0000000000..712abaf756
--- /dev/null
+++ b/challenge-219/arne-sommer/raku/sorted-squares
@@ -0,0 +1,5 @@
+#! /usr/bin/env raku
+
+unit sub MAIN (*@list where @list.elems >= 1 && all(@list) ~~ Int);
+
+say '(' ~ @list.map( * ** 2 ).sort.join(", ") ~ ')';
diff --git a/challenge-219/arne-sommer/raku/travel-expenditure b/challenge-219/arne-sommer/raku/travel-expenditure
new file mode 100755
index 0000000000..4f30e24971
--- /dev/null
+++ b/challenge-219/arne-sommer/raku/travel-expenditure
@@ -0,0 +1,77 @@
+#! /usr/bin/env raku
+
+unit sub MAIN ($costs, $days, :v(:$verbose));
+
+my @costs = $costs.words>>.Numeric;
+
+die "Specify three costs" unless @costs.elems == 3;
+die "Costs must be numeric" unless all(@costs) ~~ Numeric;
+
+my %costs = ( 1 => @costs[0], 7 => @costs[1], 30 => @costs[2] );
+
+my @days = $days.words>>.UInt;
+
+die "The days must be positive integers (1..366)" unless all(@days) ~~ UInt && 1 <= all(@days) <= 366;
+
+my @res = gather
+{
+ recurse(log => (), total-cost => 0, todo => @days.sort);
+}
+
+my $lowest = Inf;
+
+for @res -> $row
+{
+ print ": Cost: $row{'cost'}" if $verbose;
+
+ if $row{'cost'} < $lowest
+ {
+ $lowest = $row{'cost'};
+ say " - new lowest" if $verbose;
+ }
+ elsif $verbose
+ {
+ say "";
+ }
+
+ if $verbose
+ {
+ for @($row{'log'}) -> $log
+ {
+ say ": - $log";
+ }
+ say "";
+ }
+}
+
+say $lowest;
+
+sub recurse (:@log, :$total-cost, :@todo is copy)
+{
+ my $today = @todo.shift;
+
+ for 1,7,30 -> $days
+ {
+ my $paid = $total-cost + %costs{$days};
+ my $paid-to = $today + $days;
+ my @covered = ($today,);
+ my @new-todo = @todo.clone;
+ my @new-log = @log.clone;
+
+ while @new-todo.elems && @new-todo[0] < $paid-to
+ {
+ @covered.push: @new-todo.shift;
+ }
+
+ @new-log.push: "At day $today pay for $days day(s) ({ @covered.join(",") })";
+
+ if @new-todo.elems
+ {
+ recurse(log => @new-log, total-cost => $paid, todo => @new-todo)
+ }
+ else
+ {
+ take { cost => $paid, log => @new-log };
+ }
+ }
+}