aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo L. de M. Chaves <gustavo@cpqd.com.br>2021-02-15 22:12:33 -0300
committerGustavo L. de M. Chaves <gustavo@cpqd.com.br>2021-02-15 22:12:33 -0300
commitd01fda7f4dbd80462485dc204b2865c9d0377079 (patch)
tree61255591c8d8085071439a9d6555d3a7958c56fd
parentdf9a999a83bb0cc92a5e260ac246604b69c511e7 (diff)
downloadperlweeklychallenge-club-d01fda7f4dbd80462485dc204b2865c9d0377079.tar.gz
perlweeklychallenge-club-d01fda7f4dbd80462485dc204b2865c9d0377079.tar.bz2
perlweeklychallenge-club-d01fda7f4dbd80462485dc204b2865c9d0377079.zip
Add Gustavo Chaves's solutions to challenge 100
-rwxr-xr-xchallenge-100/gustavo-chaves/perl/ch-1.pl44
-rwxr-xr-xchallenge-100/gustavo-chaves/perl/ch-2.pl41
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-100/gustavo-chaves/perl/ch-1.pl b/challenge-100/gustavo-chaves/perl/ch-1.pl
new file mode 100755
index 0000000000..0e9a618526
--- /dev/null
+++ b/challenge-100/gustavo-chaves/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-100/
+# TASK #1 › Fun Time
+
+use 5.030;
+use warnings;
+
+my $input = shift;
+
+if ($input =~ /^(?<hour>\d{2}):(?<minute>\d{2})$/) {
+ if ($+{hour} eq '00') {
+ print '12';
+ } elsif ($+{hour} le '12') {
+ print $+{hour};
+ } else {
+ printf '%02d', $+{hour} - 12;
+ }
+ print ":$+{minute}";
+ if ($+{hour} lt '12') {
+ print 'am';
+ } else {
+ print 'pm';
+ }
+} elsif ($input =~ /^(?<hour>\d{2}):(?<minute>\d{2}) ?(?<period>am|pm)$/) {
+ if ($+{period} eq 'am') {
+ if ($+{hour} eq '12') {
+ print '00';
+ } else {
+ print $+{hour};
+ }
+ } else {
+ if ($+{hour} eq '12') {
+ print $+{hour};
+ } else {
+ printf '%02d', $+{hour} + 12;
+ }
+ }
+ print ":$+{minute}";
+} else {
+ die "Invalid time '$input'\n";
+}
+
+print "\n";
diff --git a/challenge-100/gustavo-chaves/perl/ch-2.pl b/challenge-100/gustavo-chaves/perl/ch-2.pl
new file mode 100755
index 0000000000..e3231e4f5b
--- /dev/null
+++ b/challenge-100/gustavo-chaves/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-100/
+# TASK #2 › Triangle Sum
+
+use 5.030;
+use warnings;
+
+my @triangles = (
+ [ [1], [2,4], [6,4,9], [5,1,7,2] ],
+ [ [3], [3,1], [5,2,3], [4,3,1,3] ],
+);
+
+for my $i (0 .. $#triangles) {
+ say "$i: ", triangle_sum($triangles[$i]);
+}
+
+sub triangle_sum {
+ my ($triangle) = @_;
+
+ my $min = 2 ** 30;
+
+ my $find_minimum_sum = sub {
+ my ($row, $col, $acc) = @_;
+
+ $acc += $triangle->[$row][$col];
+
+ if ($row == $#$triangle) {
+ $min = $acc if $acc < $min;
+ } elsif ($min > $acc) {
+ __SUB__->($row+1, $col, $acc);
+ __SUB__->($row+1, $col+1, $acc);
+ }
+ return;
+ };
+
+ $find_minimum_sum->(0, 0, 0);
+
+ return $min;
+}
+