aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-06-08 12:00:46 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-06-08 12:00:46 +0200
commit0e8a9a28fb78db2f8daaa7cc882b191cfbbb95b4 (patch)
treece5a805fbb95db16454efaf9ab547d94eac12eb3
parentc4708467abd49747d2a660d44bcbecf07d0c52b7 (diff)
downloadperlweeklychallenge-club-0e8a9a28fb78db2f8daaa7cc882b191cfbbb95b4.tar.gz
perlweeklychallenge-club-0e8a9a28fb78db2f8daaa7cc882b191cfbbb95b4.tar.bz2
perlweeklychallenge-club-0e8a9a28fb78db2f8daaa7cc882b191cfbbb95b4.zip
Task 1 done
-rw-r--r--challenge-064/luca-ferrari/raku/ch-1.p670
1 files changed, 70 insertions, 0 deletions
diff --git a/challenge-064/luca-ferrari/raku/ch-1.p6 b/challenge-064/luca-ferrari/raku/ch-1.p6
new file mode 100644
index 0000000000..bdce99d3a6
--- /dev/null
+++ b/challenge-064/luca-ferrari/raku/ch-1.p6
@@ -0,0 +1,70 @@
+#!raku
+
+
+# Given an m × n matrix with non-negative integers,
+# write a script to find a path from top left to bottom
+# right which minimizes the sum of all numbers along its path.
+# You can only move either down or right at any point in time.
+# Example
+#
+# Input:
+#
+# [ 1 2 3 ]
+# [ 4 5 6 ]
+# [ 7 8 9 ]
+#
+# The minimum sum path looks like this:
+#
+# 1→2→3
+# ↓
+# 6
+# ↓
+# 9
+#
+
+
+class Node {
+ has $.position;
+ has $.value;
+
+ has Node $.right is rw;
+ has Node $.down is rw;
+
+ method adjust( @matrix, $m, $n ){
+ my $right = $!position + 1;
+ my $down = $!position + $m;
+ $!right = Node.new( :position( $right ), :value( @matrix[ $right ] ) ) if $right < $m;
+ $!down = Node.new( :position( $down ), :value( @matrix[ $down ] ) ) if $down <= $n * $m - 1;
+
+ $!right.adjust: @matrix, $m, $n if $!right;
+ $!down.adjust: @matrix, $m, $n if $!down;
+ }
+
+ method get-next-min-path(){
+ return $!right if $!right && $!down && $!right.value < $!down.value;
+ return $!down;
+ }
+}
+
+
+
+
+
+sub MAIN(){
+ my @matrix = 1, 2, 3, 4, 5, 6, 7, 8, 9;
+ my $m = 3;
+ my $n = 3;
+
+
+ my $start = Node.new( :position( 0 ), :value( @matrix[ 0 ] ) );
+ $start.adjust: @matrix, $m, $n;
+ my $current-node = $start;
+ my $sum = 0;
+ my @moves;
+ while ( $current-node ) {
+ @moves.push: $current-node.value;
+ $current-node = $current-node.get-next-min-path;
+ }
+
+ say @moves.join( ', ' ) ~ " = " ~ [+] @moves;
+}