aboutsummaryrefslogtreecommitdiff
path: root/challenge-064
diff options
context:
space:
mode:
authorYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-06-21 00:34:10 +0800
committerYsmael Ebreo <Ysmael.Ebreo@latticesemi.com>2020-06-21 00:34:10 +0800
commit09b75f558ddcb169de6c9c6d48bafbf5c3db85cd (patch)
tree185861655625e9c679fc6d56aaed3c28b2567a7f /challenge-064
parentf393438a7913c55d8987547a61638c4f78835646 (diff)
downloadperlweeklychallenge-club-09b75f558ddcb169de6c9c6d48bafbf5c3db85cd.tar.gz
perlweeklychallenge-club-09b75f558ddcb169de6c9c6d48bafbf5c3db85cd.tar.bz2
perlweeklychallenge-club-09b75f558ddcb169de6c9c6d48bafbf5c3db85cd.zip
Added perl solution ch#64
Diffstat (limited to 'challenge-064')
-rw-r--r--challenge-064/yet-ebreo/perl/ch-1.pl75
-rw-r--r--challenge-064/yet-ebreo/perl/ch-2.pl13
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-064/yet-ebreo/perl/ch-1.pl b/challenge-064/yet-ebreo/perl/ch-1.pl
new file mode 100644
index 0000000000..809f2b4b88
--- /dev/null
+++ b/challenge-064/yet-ebreo/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use feature 'say';
+
+
+my @array = (
+ # [1, 2, 3],
+ # [4, 5, 6],
+ # [7, 8, 9],
+ [qw(1 0 0 9 9)],
+ [qw(9 9 0 9 9)],
+ [qw(9 0 0 9 9)],
+ [qw(9 0 9 9 9)],
+ [qw(9 0 0 0 1)],
+);
+
+my $min_sum = 1e9;
+my $min_path = "";
+my $max_x = $#{$array[0]};
+my $max_y = $#array;
+
+sub traverse {
+ my ($arr, $x, $y, $sum, $path, $coords) = @_;
+
+ # say $coords;
+ if ($x == $max_x && $y == $max_y ) {
+ # say "$sum: $path";
+ if ($sum < $min_sum) {
+ $min_sum = $sum;
+ $min_path = $path;
+ }
+ }
+
+ #right
+ if ($x+1 <= $max_x) {
+
+ my $xy = "[$y:".($x+1)."]";
+ if ($coords !~ /\Q$xy/) {
+ traverse ($arr, $x+1, $y, $sum+$arr->[$y][$x+1] , $path . " > " . $arr->[$y][$x+1], $coords.$xy)
+ }
+
+ }
+
+ #down
+ if ($y+1 <= $max_y) {
+
+ my $xy = "[".($y+1).":$x]";
+ if ($coords !~ /\Q$xy/) {
+ traverse ($arr, $x, $y+1, $sum+$arr->[$y+1][$x] , $path . " > " . $arr->[$y+1][$x], $coords.$xy)
+ }
+
+ }
+
+ #left
+ if ($x-1 >= 0) {
+ my $xy = "[$y:".($x-1)."]";
+ if ($coords !~ /\Q$xy/) {
+ traverse ($arr, $x-1, $y, $sum+$arr->[$y][$x-1] , $path . " > " . $arr->[$y][$x-1], $coords.$xy)
+ }
+ }
+
+ #up
+ if ($y-1 >= 0) {
+ my $xy = "[".($y-1).":$x]";
+ if ($coords !~ /\Q$xy/) {
+ traverse ($arr, $x, $y-1, $sum+$arr->[$y-1][$x] , $path . " > " . $arr->[$y-1][$x], $coords.$xy)
+ }
+ }
+}
+
+traverse (\@array, 0, 0, $array[0][0], $array[0][0], "[0:0]");
+
+say $min_path;
+say "Sum is: $min_sum"
diff --git a/challenge-064/yet-ebreo/perl/ch-2.pl b/challenge-064/yet-ebreo/perl/ch-2.pl
new file mode 100644
index 0000000000..7af3d53329
--- /dev/null
+++ b/challenge-064/yet-ebreo/perl/ch-2.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use feature 'say';
+
+my $string = "perlweeklychallenge";
+my @words = qw(perl weekly challenge);
+my @ret = ();
+for my $word (@words) {
+ $string =~ s/$word/ / && push @ret, $word
+}
+
+say $string =~ /\S/ ? 0 : "@ret"