diff options
| author | Mark Anderson <mark@frontrangerunner.com> | 2020-11-18 19:42:39 -0700 |
|---|---|---|
| committer | Mark Anderson <mark@frontrangerunner.com> | 2020-11-18 19:42:39 -0700 |
| commit | 30c0f49945f99fccca6170165621e39e791a5bb5 (patch) | |
| tree | ae306537a688ced2fc0c0dfdf349b8ce22fef039 | |
| parent | 558fce8644accd4abe206482c872ee0ecb444187 (diff) | |
| download | perlweeklychallenge-club-30c0f49945f99fccca6170165621e39e791a5bb5.tar.gz perlweeklychallenge-club-30c0f49945f99fccca6170165621e39e791a5bb5.tar.bz2 perlweeklychallenge-club-30c0f49945f99fccca6170165621e39e791a5bb5.zip | |
ch-2.p6
| -rw-r--r-- | challenge-087/mark-anderson/raku/ch-1.p6 | 43 | ||||
| -rw-r--r-- | challenge-087/mark-anderson/raku/ch-2.p6 | 79 |
2 files changed, 79 insertions, 43 deletions
diff --git a/challenge-087/mark-anderson/raku/ch-1.p6 b/challenge-087/mark-anderson/raku/ch-1.p6 deleted file mode 100644 index e2e9753b02..0000000000 --- a/challenge-087/mark-anderson/raku/ch-1.p6 +++ /dev/null @@ -1,43 +0,0 @@ -multi MAIN(*@n where .all ~~ Int) { - my @seqs = get-longest-seqs(@n); - - @seqs = @seqs.map(*.join(", ").List); - - .say for @seqs or 0; -} - -multi MAIN { - use Test; - plan 3; - - cmp-ok get-longest-seqs([100, 4, 50, 3, 2]), - &[eqv], [(2, 3, 4),], "Example 1"; - - cmp-ok get-longest-seqs([20, 30, 10, 40, 50]), - &[eqv], [], "Example 2"; - - cmp-ok get-longest-seqs([20, 19, 9, 11, 10]), - &[eqv], [(9, 10, 11),], "Example 3"; -} - -sub get-longest-seqs(@n) { - my %h; - @n = @n.sort.unique; - - while @n { - my $seq = @n.splice(0, get-index(@n)); - %h{$seq} = $seq.elems if $seq > 1; - } - - sub get-index(@n) { - for ^@n -> $i { - unless @n[0..$i] == @n[0]..@n[$i] { - return $i; - } - } - - return @n.elems; - } - - [gather take .key.split(/\s/).map(+*).List for %h.maxpairs.sort]; -} diff --git a/challenge-087/mark-anderson/raku/ch-2.p6 b/challenge-087/mark-anderson/raku/ch-2.p6 new file mode 100644 index 0000000000..795f1235ea --- /dev/null +++ b/challenge-087/mark-anderson/raku/ch-2.p6 @@ -0,0 +1,79 @@ +use Test; +plan 4; + +my @example1 = < 0 0 0 1 0 1 >, + < 1 1 1 0 0 0 >, + < 0 0 1 0 0 1 >, + < 1 1 1 1 1 0 >, + < 1 1 1 1 1 0 >; + +my @example2 = < 1 0 1 0 1 0 >, + < 0 1 0 1 0 1 >, + < 1 0 1 0 1 0 >, + < 0 1 0 1 0 1 >; + +my @example3 = < 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 >; + +my @example4 = < 0 0 1 1 >, + < 0 0 1 1 >, + < 0 0 1 1 >, + < 0 0 1 1 >, + < 0 0 1 1 >, + < 0 0 1 1 >, + < 0 0 1 1 >, + < 0 0 1 1 >; + +cmp-ok get-max-rectangle(@example1), &[eqv], [[1,1,1,1,1] xx 2], "Example 1"; + +ok get-max-rectangle(@example2) == 0, "Example 2"; + +cmp-ok get-max-rectangle(@example3), &[eqv], [[1,1,1,1] xx 2], "Example 3"; + +cmp-ok get-max-rectangle(@example4), &[eqv], [[1,1] xx 8], "Example 4"; + +sub get-max-rectangle(@matrix) { + my @drawing; + my @max-rectangle; + my $max-area = 0; + my $cols = @matrix[0].elems; + my @histogram = 0 xx $cols; + + for @matrix -> @row { + for ^@row -> $i { + @histogram[$i] = @row[$i] ?? @histogram[$i]+1 !! 0; + } + + get-drawing(); + check-for-rectangles(); + } + + return @max-rectangle || 0; + + sub get-drawing { + @drawing = Empty; + for @histogram.max-1...0 -> $r { + for ^$cols -> $c { + @drawing.push: @histogram[$c] > $r ?? 1 !! 0; + } + } + @drawing = @drawing.rotor($cols).map(*.join); + } + + sub check-for-rectangles { + for ^@drawing.end -> $i { + for @drawing[$i] ~~ m:g/1+/ -> $m { + my $width = $m.to - $m.from; + my $height = min @histogram[$m.from..$m.to-1]; + my $area = $width * $height; + if $area > $max-area { + $max-area = $area; + @max-rectangle = [1 xx $width] xx $height; + } + } + } + } +} |
