aboutsummaryrefslogtreecommitdiff
path: root/challenge-248
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2023-12-18 17:39:06 -0500
committerDave Jacoby <jacoby.david@gmail.com>2023-12-18 17:39:06 -0500
commit09865411858cb259b6c509deae5de4bb589a5ade (patch)
tree3a81e4bb2dcf4e24fe58b4e9433ae3d37166e3f1 /challenge-248
parentf20e269818207c1e25f7c53100ee170beaa780a6 (diff)
downloadperlweeklychallenge-club-09865411858cb259b6c509deae5de4bb589a5ade.tar.gz
perlweeklychallenge-club-09865411858cb259b6c509deae5de4bb589a5ade.tar.bz2
perlweeklychallenge-club-09865411858cb259b6c509deae5de4bb589a5ade.zip
solved 248
Diffstat (limited to 'challenge-248')
-rw-r--r--challenge-248/dave-jacoby/perl/ch-1.pl56
-rw-r--r--challenge-248/dave-jacoby/perl/ch-2.pl59
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-248/dave-jacoby/perl/ch-1.pl b/challenge-248/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..ebadc812d8
--- /dev/null
+++ b/challenge-248/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ min };
+
+my @examples = (
+
+ { str => "loveleetcode", char => "e" },
+ { str => "aaab", char => "b" },
+
+);
+
+for my $example (@examples) {
+ my @output = shortest_distance($example);
+ my $output = join ',', @output;
+
+ # my $input = join ",\n\t", map { qq{"$_"} } # quote surname
+ # map { $_->[0] } # remove surname element
+ # sort { $a->[1] cmp $b->[1] } # sort on surname
+ # map { [ $_, ( reverse split /\s/, $_ )[0] ]
+ # } # start schartzian transform on surname
+ # sort { $a cmp $b } $example->@*; # sort alphabetically for consistency
+ # my $output = join "\n\t",
+ # map { qq{$_ -> $output{$_}} } # combine santa and giftee
+ # map { $_->[0] } # remove surname element
+ # sort { $a->[1] cmp $b->[1] } # sort on surname
+ # map {
+ # [ $_, ( reverse split /\s/, $_ )[0] ]
+ # } # start schartzian transform on surname
+ # sort { $a cmp $b } keys %output; # sort alphabetically for consistency
+
+ say <<~"END";
+ Input: \$str = "$example->{str}", \$char = "$example->{char}"
+ Output: ($output)
+ END
+
+}
+
+# 1) everybody gets matched
+# 2) nobody gets matched to themself
+sub shortest_distance ($input) {
+ my $str = $input->{str};
+ my $char = $input->{char};
+ my @input = grep { $char eq substr $str, $_, 1 } 0 .. length $str;
+ my @output;
+
+ for my $i ( 0 .. -1 + length $str ) {
+ my $c = substr $str, $i, 1;
+ push @output, min map { abs $_ - $i } @input;
+ }
+
+ return @output;
+}
diff --git a/challenge-248/dave-jacoby/perl/ch-2.pl b/challenge-248/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..f0e0a58cc4
--- /dev/null
+++ b/challenge-248/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,59 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ max sum };
+
+my @examples = (
+ [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ],
+ [ [ 1, 0, 0, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ] ]
+);
+
+for my $e (@examples) {
+ my $o = submatrix_sum($e);
+ my $input = format_matrix($e);
+ my $output = format_matrix($o);
+
+ say <<~"END";
+ Input: \$a = $input
+
+ Output: \$b = $output
+ END
+}
+
+sub submatrix_sum ($m) {
+ my @output;
+ for my $x ( 0 .. -2 + scalar $m->@* ) {
+ for my $y ( 0 .. -2 + scalar $m->[$x]->@* ) {
+ my @z;
+ push @z, $m->[ $x + 0 ][ $y + 0 ];
+ push @z, $m->[ $x + 1 ][ $y + 0 ];
+ push @z, $m->[ $x + 0 ][ $y + 1 ];
+ push @z, $m->[ $x + 1 ][ $y + 1 ];
+ my $z = sum @z;
+ $output[$x][$y] = $z;
+ }
+ }
+ return \@output;
+}
+
+sub format_matrix ($matrix) {
+ my $maxlen = max map { length $_ } map { $_->@* } $matrix->@*;
+ my $output = join "\n ", '[', (
+ map { qq{ [$_],} }
+ map {
+ join ',',
+ map { pad( $_, 1 + $maxlen ) }
+ $_->@*
+ }
+ map { $matrix->[$_] } 0 .. -1 + scalar $matrix->@*
+ ),
+ ']';
+ return $output;
+}
+
+sub pad ( $str, $len = 4 ) {
+ return sprintf "%${len}s", $str;
+}