diff options
| author | arnesom <arne@bbop.org> | 2020-11-21 20:34:27 +0100 |
|---|---|---|
| committer | arnesom <arne@bbop.org> | 2020-11-21 20:34:27 +0100 |
| commit | ff1839a0be1306e6c90c9cc98c14feb241957421 (patch) | |
| tree | af0a4ffa5b93d0c354f626901f365e8813a81d9b | |
| parent | 3515458c7a0532e4b4475b3ad4b34e4e92b7dc99 (diff) | |
| download | perlweeklychallenge-club-ff1839a0be1306e6c90c9cc98c14feb241957421.tar.gz perlweeklychallenge-club-ff1839a0be1306e6c90c9cc98c14feb241957421.tar.bz2 perlweeklychallenge-club-ff1839a0be1306e6c90c9cc98c14feb241957421.zip | |
Arne Sommer 087
| -rw-r--r-- | challenge-087/arne-sommer/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-087/arne-sommer/example1.txt | 5 | ||||
| -rw-r--r-- | challenge-087/arne-sommer/example2.txt | 4 | ||||
| -rw-r--r-- | challenge-087/arne-sommer/example3.txt | 5 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/perl/ch-1.pl | 38 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/perl/longest-consecutive-sequence-perl | 38 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/raku/ch-1.p6 | 33 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/raku/ch-2.p6 | 127 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/raku/largest-rectangle | 126 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/raku/largest-rectangle-cleanup | 57 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/raku/largest-rectangle2 | 127 | ||||
| -rwxr-xr-x | challenge-087/arne-sommer/raku/longest-consecutive-sequence | 33 |
12 files changed, 594 insertions, 0 deletions
diff --git a/challenge-087/arne-sommer/blog.txt b/challenge-087/arne-sommer/blog.txt new file mode 100644 index 0000000000..0260900100 --- /dev/null +++ b/challenge-087/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/largest-longest.html diff --git a/challenge-087/arne-sommer/example1.txt b/challenge-087/arne-sommer/example1.txt new file mode 100644 index 0000000000..14c0fc6cf9 --- /dev/null +++ b/challenge-087/arne-sommer/example1.txt @@ -0,0 +1,5 @@ +[ 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 ] diff --git a/challenge-087/arne-sommer/example2.txt b/challenge-087/arne-sommer/example2.txt new file mode 100644 index 0000000000..34863d95ed --- /dev/null +++ b/challenge-087/arne-sommer/example2.txt @@ -0,0 +1,4 @@ +[ 1 0 1 0 1 0 ] +[ 0 1 0 1 0 1 ] +[ 1 0 1 0 1 0 ] +[ 0 1 0 1 0 1 ] diff --git a/challenge-087/arne-sommer/example3.txt b/challenge-087/arne-sommer/example3.txt new file mode 100644 index 0000000000..b34017aebc --- /dev/null +++ b/challenge-087/arne-sommer/example3.txt @@ -0,0 +1,5 @@ +[ 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 ] diff --git a/challenge-087/arne-sommer/perl/ch-1.pl b/challenge-087/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..d767c8d88b --- /dev/null +++ b/challenge-087/arne-sommer/perl/ch-1.pl @@ -0,0 +1,38 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use List::Util qw(all uniq); + +die "At least 1 value" unless @ARGV; +die "Integers only" unless all { $ ~= /^\-?\d+$/ } @ARGV; + +my @sorted = uniq sort { $a <=> $b } @ARGV; + +my @longest; +my $longest = 0; +my @current = shift(@sorted); +my $current = $current[0]; + +for my $num (@sorted) +{ + if ($num == $current + 1) + { + push(@current, $num); + $current = $num; + } + else + { + if (@current > $longest) + { + @longest = @current; + $longest = @longest; + } + $current = $num; + @current = ($num); + } +} + +say @longest > 1 + ? "(" . join(", ", @longest) . ")" + : "0"; diff --git a/challenge-087/arne-sommer/perl/longest-consecutive-sequence-perl b/challenge-087/arne-sommer/perl/longest-consecutive-sequence-perl new file mode 100755 index 0000000000..d767c8d88b --- /dev/null +++ b/challenge-087/arne-sommer/perl/longest-consecutive-sequence-perl @@ -0,0 +1,38 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use List::Util qw(all uniq); + +die "At least 1 value" unless @ARGV; +die "Integers only" unless all { $ ~= /^\-?\d+$/ } @ARGV; + +my @sorted = uniq sort { $a <=> $b } @ARGV; + +my @longest; +my $longest = 0; +my @current = shift(@sorted); +my $current = $current[0]; + +for my $num (@sorted) +{ + if ($num == $current + 1) + { + push(@current, $num); + $current = $num; + } + else + { + if (@current > $longest) + { + @longest = @current; + $longest = @longest; + } + $current = $num; + @current = ($num); + } +} + +say @longest > 1 + ? "(" . join(", ", @longest) . ")" + : "0"; diff --git a/challenge-087/arne-sommer/raku/ch-1.p6 b/challenge-087/arne-sommer/raku/ch-1.p6 new file mode 100755 index 0000000000..4976d2a509 --- /dev/null +++ b/challenge-087/arne-sommer/raku/ch-1.p6 @@ -0,0 +1,33 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@N where @N.elems && all(@N) ~~ Int); + +my @sorted = @N.sort.squish; + +my @longest; +my $longest = 0; +my @current = @sorted.shift; +my $current = @current[0]; + +for @sorted -> $num +{ + if $num == $current + 1 + { + @current.push: $num; + $current = $num; + } + else + { + if (@current.elems > $longest) + { + @longest = @current; + $longest = @longest.elems; + } + $current = $num; + @current = ($num); + } +} + +say @longest.elems > 1 + ?? "({ @longest.join(", ") })" + !! "0"; diff --git a/challenge-087/arne-sommer/raku/ch-2.p6 b/challenge-087/arne-sommer/raku/ch-2.p6 new file mode 100755 index 0000000000..4ec62be23e --- /dev/null +++ b/challenge-087/arne-sommer/raku/ch-2.p6 @@ -0,0 +1,127 @@ +#! /usr/bin/env raku + +subset BinaryDigit where /^<[01]>$/; + +unit sub MAIN ($rectangle where $rectangle.IO.f && $rectangle.IO.r = 'sample1.txt', + :v(:$verbose), :s(:$show), :h($html)); + +my @rectangle = $rectangle.IO.lines.map( *.words.grep(BinaryDigit) )>>.Array; + +my $rows = @rectangle.elems; +my $cols = @rectangle[0].elems; + +die "All rows must have the same length" unless [==] @(@rectangle)>>.elems; + +my $col-blue = "\e[44m"; +my $col-green = "\e[42m"; +my $col-red = "\e[101m"; +my $col-stop = "\e[0m"; + +if ($html) +{ + $col-blue = '<span class="text-light bg-primary">'; + $col-green = '<span class="text-light bg-success">'; + $col-red = '<span class="text-light bg-danger">'; + $col-stop = '</span>'; +} + +my @partials; + +class partial +{ + has $.start-row; + has $.start-col; + has $.stop-row; + has $.stop-col; + + method size + { + return (self.stop-row - self.start-row + 1) * (self.stop-col - self.start-col + 1); + } + + method all-one + { + for self.start-row .. self.stop-row -> $row + { + for self.start-col .. self.stop-col -> $col + { + return False if @rectangle[$row][$col] == 0; + } + } + return True; + } + + method debug-output + { + return ": [{ self.start-row },{ self.start-col } -> { self.stop-row },{ self.stop-col }] -> { self.size } -> { self.all-one }"; + } + + method show + { + for self.start-row .. self.stop-row -> $row + { + print "[ "; + for self.start-col .. self.stop-col -> $col + { + print @rectangle[$row][$col] ~ " "; + } + say "]"; + } + } +} + +for 0 .. $rows -1 -> $start-row +{ + for 0 .. $cols -1 -> $start-col + { + if @rectangle[$start-row][$start-col] == 0 + { + say ": [$start-row,$start-col] skipped (is 0)" if $verbose; + next; + } + + for $start-row +1 .. $rows -1 -> $stop-row + { + for $start-col +1 .. $cols -1 -> $stop-col + { + my $partial = partial.new(:$start-row, :$stop-row, :$start-col, :$stop-col); + $partial.debug-output.say if $verbose; + @partials.push: $partial if $partial.all-one; + } + } + } +} + +say "" if $verbose; + +my @sorted = @partials.sort({ $^b.size <=> $^a.size }); + +if @sorted +{ + @sorted>>.debug-output>>.say if $verbose; + + my $candidate = @sorted[0]; + + coloured-matrix($candidate.start-row, $candidate.start-col, $candidate.stop-row, $candidate.stop-col) if $show; + say "" if $verbose || $show; + $candidate.show; + exit; +} + +sub coloured-matrix ($start-row, $start-col, $stop-row, $stop-col) +{ + for ^$rows -> $row + { + print "[ "; + for ^$cols -> $col + { + print $start-row <= $row <= $stop-row && $start-col <= $col <= $stop-col + ?? ( $col-green ~ @rectangle[$row][$col] ~ $col-stop ~ " " ) + !! ( @rectangle[$row][$col] ~ " " ); + + } + say "]"; + } +} + +say 0; diff --git a/challenge-087/arne-sommer/raku/largest-rectangle b/challenge-087/arne-sommer/raku/largest-rectangle new file mode 100755 index 0000000000..6c9a24d2be --- /dev/null +++ b/challenge-087/arne-sommer/raku/largest-rectangle @@ -0,0 +1,126 @@ +#! /usr/bin/env raku + +subset BinaryDigit where /^<[01]>$/; + +unit sub MAIN ($rectangle where $rectangle.IO.f && $rectangle.IO.r = 'sample1.txt', + :v(:$verbose), :s(:$show), :h($html)); + +my @rectangle = $rectangle.IO.lines.map( *.words.grep(BinaryDigit) )>>.Array; + +my $rows = @rectangle.elems; +my $cols = @rectangle[0].elems; + +die "All rows must have the same length" unless [==] @(@rectangle)>>.elems; + +my $col-blue = "\e[44m"; +my $col-green = "\e[42m"; +my $col-red = "\e[101m"; +my $col-stop = "\e[0m"; + +if ($html) +{ + $col-blue = '<span class="text-light bg-primary">'; + $col-green = '<span class="text-light bg-success">'; + $col-red = '<span class="text-light bg-danger">'; + $col-stop = '</span>'; +} + +my @partials; + +class partial +{ + has $.start-row; + has $.start-col; + has $.stop-row; + has $.stop-col; + + method size + { + return (self.stop-row - self.start-row + 1) * (self.stop-col - self.start-col + 1); + } + + method all-one + { + for self.start-row .. self.stop-row -> $row + { + for self.start-col .. self.stop-col -> $col + { + return False if @rectangle[$row][$col] == 0; + } + } + return True; + } + + method debug-output + { + return ": [{ self.start-row },{ self.start-col } -> { self.stop-row },{ self.stop-col }] -> { self.size } -> { self.all-one }"; + } + + method show + { + for self.start-row .. self.stop-row -> $row + { + print "[ "; + for self.start-col .. self.stop-col -> $col + { + print @rectangle[$row][$col] ~ " "; + } + say "]"; + } + } +} + +for 0 .. $rows -1 -> $start-row +{ + for 0 .. $cols -1 -> $start-col + { + if @rectangle[$start-row][$start-col] == 0 + { + say ": [$start-row,$start-col] skipped (is 0)" if $verbose; + next; + } + + for $start-row +1 .. $rows -1 -> $stop-row + { + for $start-col +1 .. $cols -1 -> $stop-col + { + @partials.push(partial.new(:$start-row, :$stop-row, :$start-col, :$stop-col)); + @partials[*-1].debug-output.say if $verbose; + } + } + } +} + +say "" if $verbose; + +my @sorted = @partials.sort({ $^b.size <=> $^a.size }); + +for @sorted -> $candidate +{ + $candidate.debug-output.say if $verbose; + if $candidate.all-one + { + coloured-matrix($candidate.start-row, $candidate.start-col, $candidate.stop-row, $candidate.stop-col) if $show; + say "" if $verbose || $show; + $candidate.show; + exit; + } +} + +sub coloured-matrix ($start-row, $start-col, $stop-row, $stop-col) +{ + for ^$rows -> $row + { + print "[ "; + for ^$cols -> $col + { + print $start-row <= $row <= $stop-row && $start-col <= $col <= $stop-col + ?? ( $col-green ~ @rectangle[$row][$col] ~ $col-stop ~ " " ) + !! ( @rectangle[$row][$col] ~ " " ); + + } + say "]"; + } +} + +say 0; diff --git a/challenge-087/arne-sommer/raku/largest-rectangle-cleanup b/challenge-087/arne-sommer/raku/largest-rectangle-cleanup new file mode 100755 index 0000000000..12199ca816 --- /dev/null +++ b/challenge-087/arne-sommer/raku/largest-rectangle-cleanup @@ -0,0 +1,57 @@ +#! /usr/bin/env raku + +subset BinaryDigit where /^<[01]>$/; + +unit sub MAIN ($rectangle where $rectangle.IO.f && $rectangle.IO.r = 'sample1.txt', + :v(:$verbose)); + +my @rectangle = $rectangle.IO.lines.map( *.words.grep(BinaryDigit) )>>.Array; + +my $rows = @rectangle.elems; +my $cols = @rectangle[0].elems; + +die "All rows must have the same length" unless [==] @(@rectangle)>>.elems; + +my $pass = 1; +my $sum = @rectangle>>.sum.sum; + +LOOP: loop +{ + say ": Pass: { $pass++ }" if $verbose; + + for ^$rows -> $row + { + for ^$cols -> $col + { + next if @rectangle[$row][$col] == 0; + + next if box4($row -1, $col -1) || box4($row, $col -1) || + box4($row -1, $col ) || box4($row, $col ); + + @rectangle[$row][$col] = 0; + } + } + + my $new-sum = @rectangle>>.sum.sum; + last if $new-sum == $sum; + $sum = $new-sum; +} + +say @$_ for @rectangle; + +sub box4 ($row, $col) +{ + say ": box4($row, $col)" if $verbose; + my $a = @rectangle[$row ][$col ] // return False; + my $b = @rectangle[$row+1][$col ] // return False; + my $c = @rectangle[$row ][$col+1] // return False; + my $d = @rectangle[$row+1][$col+1] // return False; + + if $a + $b + $c + $d == 4 + { + say ": - ok" if $verbose; + return True; + } + + return False; +} diff --git a/challenge-087/arne-sommer/raku/largest-rectangle2 b/challenge-087/arne-sommer/raku/largest-rectangle2 new file mode 100755 index 0000000000..4ec62be23e --- /dev/null +++ b/challenge-087/arne-sommer/raku/largest-rectangle2 @@ -0,0 +1,127 @@ +#! /usr/bin/env raku + +subset BinaryDigit where /^<[01]>$/; + +unit sub MAIN ($rectangle where $rectangle.IO.f && $rectangle.IO.r = 'sample1.txt', + :v(:$verbose), :s(:$show), :h($html)); + +my @rectangle = $rectangle.IO.lines.map( *.words.grep(BinaryDigit) )>>.Array; + +my $rows = @rectangle.elems; +my $cols = @rectangle[0].elems; + +die "All rows must have the same length" unless [==] @(@rectangle)>>.elems; + +my $col-blue = "\e[44m"; +my $col-green = "\e[42m"; +my $col-red = "\e[101m"; +my $col-stop = "\e[0m"; + +if ($html) +{ + $col-blue = '<span class="text-light bg-primary">'; + $col-green = '<span class="text-light bg-success">'; + $col-red = '<span class="text-light bg-danger">'; + $col-stop = '</span>'; +} + +my @partials; + +class partial +{ + has $.start-row; + has $.start-col; + has $.stop-row; + has $.stop-col; + + method size + { + return (self.stop-row - self.start-row + 1) * (self.stop-col - self.start-col + 1); + } + + method all-one + { + for self.start-row .. self.stop-row -> $row + { + for self.start-col .. self.stop-col -> $col + { + return False if @rectangle[$row][$col] == 0; + } + } + return True; + } + + method debug-output + { + return ": [{ self.start-row },{ self.start-col } -> { self.stop-row },{ self.stop-col }] -> { self.size } -> { self.all-one }"; + } + + method show + { + for self.start-row .. self.stop-row -> $row + { + print "[ "; + for self.start-col .. self.stop-col -> $col + { + print @rectangle[$row][$col] ~ " "; + } + say "]"; + } + } +} + +for 0 .. $rows -1 -> $start-row +{ + for 0 .. $cols -1 -> $start-col + { + if @rectangle[$start-row][$start-col] == 0 + { + say ": [$start-row,$start-col] skipped (is 0)" if $verbose; + next; + } + + for $start-row +1 .. $rows -1 -> $stop-row + { + for $start-col +1 .. $cols -1 -> $stop-col + { + my $partial = partial.new(:$start-row, :$stop-row, :$start-col, :$stop-col); + $partial.debug-output.say if $verbose; + @partials.push: $partial if $partial.all-one; + } + } + } +} + +say "" if $verbose; + +my @sorted = @partials.sort({ $^b.size <=> $^a.size }); + +if @sorted +{ + @sorted>>.debug-output>>.say if $verbose; + + my $candidate = @sorted[0]; + + coloured-matrix($candidate.start-row, $candidate.start-col, $candidate.stop-row, $candidate.stop-col) if $show; + say "" if $verbose || $show; + $candidate.show; + exit; +} + +sub coloured-matrix ($start-row, $start-col, $stop-row, $stop-col) +{ + for ^$rows -> $row + { + print "[ "; + for ^$cols -> $col + { + print $start-row <= $row <= $stop-row && $start-col <= $col <= $stop-col + ?? ( $col-green ~ @rectangle[$row][$col] ~ $col-stop ~ " " ) + !! ( @rectangle[$row][$col] ~ " " ); + + } + say "]"; + } +} + +say 0; diff --git a/challenge-087/arne-sommer/raku/longest-consecutive-sequence b/challenge-087/arne-sommer/raku/longest-consecutive-sequence new file mode 100755 index 0000000000..4976d2a509 --- /dev/null +++ b/challenge-087/arne-sommer/raku/longest-consecutive-sequence @@ -0,0 +1,33 @@ +#! /usr/bin/env raku + +unit sub MAIN (*@N where @N.elems && all(@N) ~~ Int); + +my @sorted = @N.sort.squish; + +my @longest; +my $longest = 0; +my @current = @sorted.shift; +my $current = @current[0]; + +for @sorted -> $num +{ + if $num == $current + 1 + { + @current.push: $num; + $current = $num; + } + else + { + if (@current.elems > $longest) + { + @longest = @current; + $longest = @longest.elems; + } + $current = $num; + @current = ($num); + } +} + +say @longest.elems > 1 + ?? "({ @longest.join(", ") })" + !! "0"; |
