aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarkus "Holli" Holzer <holli.holzer@gmail.com>2020-04-12 13:05:34 +0200
committerMarkus "Holli" Holzer <holli.holzer@gmail.com>2020-04-12 13:05:34 +0200
commitd1c2461aaa091e01c9a897b2901d54c97e80c5ae (patch)
tree20e604d6c78a00a9ee034796e4fb08c98a87059b
parentb0634f8e0d0c77c0d92de8c619b31d89a0c13a0d (diff)
downloadperlweeklychallenge-club-d1c2461aaa091e01c9a897b2901d54c97e80c5ae.tar.gz
perlweeklychallenge-club-d1c2461aaa091e01c9a897b2901d54c97e80c5ae.tar.bz2
perlweeklychallenge-club-d1c2461aaa091e01c9a897b2901d54c97e80c5ae.zip
this feels clumsy
-rw-r--r--challenge-055/markus-holzer/raku/ch-1.p643
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-055/markus-holzer/raku/ch-1.p6 b/challenge-055/markus-holzer/raku/ch-1.p6
new file mode 100644
index 0000000000..a16224933a
--- /dev/null
+++ b/challenge-055/markus-holzer/raku/ch-1.p6
@@ -0,0 +1,43 @@
+subset BinaryStr of Str where * ~~ /^ <[01]>+ $/;
+
+sub MAIN( BinaryStr $input )
+{
+ my $pad = '%0' ~ $input.chars ~ 's';
+
+ say "L: { .<l> } R: { .<r> } -> { sprintf $pad, .<flipped> }"
+ for top-flips-of( $input );
+}
+
+sub top-flips-of( BinaryStr $input )
+{
+ my %result = flips-of( $input );
+ |%result{ %result.keys.sort.tail };
+}
+
+sub flips-of( BinaryStr $input )
+{
+ my %result;
+
+ for flips( $input.chars ) -> ( $l, $r )
+ {
+ my $flipped = do-flip( $input, $l, $r );
+ my $ones = $flipped.indices( '1' ).elems;
+
+ %result{ $ones }.push({ :$l, :$r, :$flipped });
+ }
+
+ %result;
+}
+
+sub flips( Int $length )
+{
+ (^$length X ^$length).grep( -> ( $l, $r ) { $l <= $r })
+}
+
+sub do-flip( BinaryStr $input, Int $l, Int $r ) returns BinaryStr
+{
+ my @flips = ( ^$input.chars ).map({ $l <= $_ <= $r ?? 1 !! 0 });
+ my $flipped = :2( $input ) +^ :2( @flips.join );
+
+ $flipped.base(2);
+} \ No newline at end of file