aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-087/gugod/README6
-rw-r--r--challenge-087/gugod/raku/ch-1.raku35
-rw-r--r--challenge-087/gugod/raku/ch-2.raku84
3 files changed, 122 insertions, 3 deletions
diff --git a/challenge-087/gugod/README b/challenge-087/gugod/README
index 5d83e3a778..9a4443f23b 100644
--- a/challenge-087/gugod/README
+++ b/challenge-087/gugod/README
@@ -1,6 +1,6 @@
Solutions by Kang-min Liu.
Blog posts:
-- https://gugod.org/2020/11/pwc-086/
-- https://gugod.org/2020/11/pwc-086-en/
-- https://dev.to/gugod/solving-perl-weekly-challenge-086-3de0
+- https://gugod.org/2020/11/pwc-087/
+- https://gugod.org/2020/11/pwc-087-en/
+- https://dev.to/gugod/solvintg-perl-weekly-challenge-087-1f09
diff --git a/challenge-087/gugod/raku/ch-1.raku b/challenge-087/gugod/raku/ch-1.raku
new file mode 100644
index 0000000000..dda0527a09
--- /dev/null
+++ b/challenge-087/gugod/raku/ch-1.raku
@@ -0,0 +1,35 @@
+
+sub MAIN() {
+ say longest-consecutive-sequence([9,1,2,7,8,84]) // 0;
+ say longest-consecutive-sequence([2,1,3,9,7,8,4]) // 0;
+ say longest-consecutive-sequence([2,1,2,3,9,7,8,4]) // 0;
+}
+
+sub longest-consecutive-sequence(@N) {
+ my @m = @N.sort({ $^a <=> $^b });
+
+ my $seq_from = @m[0];
+ my $seq_until = @m[0];
+ my $longest_seq_from = @m[0];
+ my $longest_seq_until = @m[0];
+ my $longest_seq_length = 0;
+
+ for 1..@m.end -> $i {
+ my $n = @m[$i];
+ if $n - @m[$i-1] == 0|1 {
+ $seq_until = $n;
+ my $len = $seq_until - $seq_from;
+ if $longest_seq_length < $len {
+ $longest_seq_from = $seq_from;
+ $longest_seq_until = $seq_until;
+ $longest_seq_length = $len;
+ }
+ } else {
+ $seq_from = $n;
+ $seq_until = $n;
+ }
+ }
+
+ return $longest_seq_length == 0 ?? Nil !! [$longest_seq_from...$longest_seq_until];
+}
+
diff --git a/challenge-087/gugod/raku/ch-2.raku b/challenge-087/gugod/raku/ch-2.raku
new file mode 100644
index 0000000000..4aa9e862ff
--- /dev/null
+++ b/challenge-087/gugod/raku/ch-2.raku
@@ -0,0 +1,84 @@
+
+sub MAIN() {
+ my @examples = [
+ [
+ [ 0, 0, 0, 1, 0, 0 ],
+ [ 1, 1, 1, 0, 0, 0 ],
+ [ 0, 0, 1, 0, 0, 1 ],
+ [ 1, 1, 1, 1, 1, 0 ],
+ [ 1, 1, 1, 1, 1, 0 ],
+
+ ],
+ [
+ [ 0, 0, 0, 1, 0, 0 ],
+ [ 1, 1, 1, 0, 0, 0 ],
+ [ 0, 0, 1, 0, 0, 1 ],
+ [ 1, 1, 1, 1, 1, 0 ],
+ [ 1, 1, 1, 1, 1, 0 ],
+ ],
+ [
+ [ 0, 0, 0, 1, 1, 1 ],
+ [ 1, 1, 1, 1, 1, 1 ],
+ [ 0, 0, 1, 0, 0, 1 ],
+ [ 0, 0, 1, 1, 1, 1 ],
+ [ 0, 0, 1, 1, 1, 1 ],
+ ],
+
+ # None
+ [
+ [ 0, 0, 0, 1, 0, 1 ],
+ [ 0, 0, 1, 0, 0, 0 ],
+ [ 1, 0, 0, 0, 0, 1 ],
+ [ 0, 0, 1, 0, 1, 0 ],
+ ]
+ ];
+
+ for @examples -> @M {
+ say "Input:";
+ print-matrix(@M);
+
+ say "Output:";
+ my @m = largest-rectangle(@M);
+
+ if @m[0] == -Inf {
+ say 0;
+ } else {
+ print-matrix( @m );
+ }
+
+ say "";
+ }
+}
+
+sub print-matrix(@matrix) {
+ for ^@matrix -> $y {
+ say "[" ~ @matrix[$y].join(" ") ~ "]";
+ }
+}
+
+sub largest-rectangle(@matrix) {
+ ([^@matrix] X [^@matrix[0]])
+ .combinations(2)
+ .grep(
+ -> ($p, $q) {
+ $p[0] <= $q[0] and $p[1] <= $q[1]
+ })
+
+ .map(
+ -> ($p, $q) {
+ ($p[0] .. $q[0]).map(
+ -> $y {
+ @matrix[$y][ $p[1] .. $q[1] ]
+ })
+ })
+
+ .grep(
+ -> @m {
+ ([^@m] X [^@m[0]]).map(-> ($y,$x) { @m[$y][$x] == 1 }).all
+ })
+
+ .max(
+ -> @m {
+ @m * @m[0]
+ })
+}