aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Proctor <simon.proctor@zoopla.co.uk>2020-04-06 09:41:54 +0100
committerSimon Proctor <simon.proctor@zoopla.co.uk>2020-04-06 09:41:54 +0100
commit906abbd5a71cbcce81abf659cc7bb36ecfd06eca (patch)
tree8415dbe1658d10944e54c0041a375a2bc15b20e8
parentac3d005bd7923e9b5982785215435d2bb1a4aabc (diff)
downloadperlweeklychallenge-club-906abbd5a71cbcce81abf659cc7bb36ecfd06eca.tar.gz
perlweeklychallenge-club-906abbd5a71cbcce81abf659cc7bb36ecfd06eca.tar.bz2
perlweeklychallenge-club-906abbd5a71cbcce81abf659cc7bb36ecfd06eca.zip
Binary flippage
-rw-r--r--challenge-055/simon-proctor/raku/ch-1.p634
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-055/simon-proctor/raku/ch-1.p6 b/challenge-055/simon-proctor/raku/ch-1.p6
new file mode 100644
index 0000000000..57f812887a
--- /dev/null
+++ b/challenge-055/simon-proctor/raku/ch-1.p6
@@ -0,0 +1,34 @@
+#!/usr/bin/env raku
+
+use v6;
+
+#| Given a binary string find the start and end points for flipping the bits that results in the most 1's in the final string
+sub MAIN( Str $bin where { m!^ <[10]>+ $! } ) {
+ my @bin = $bin.comb;
+
+ my @results;
+ my $len = 0;
+
+ for 0..@bin.elems-1 -> $l {
+ for $l..@bin.elems-1 -> $r {
+ my @res = flip(@bin,$l,$r);
+ given @res.grep(* == 1).elems {
+ when * > $len {
+ $len = $_;
+ @results = [ { l => $l, r => $r, bin => @res.join("") }, ];
+ }
+ when $len {
+ @results.push( { l => $l, r => $r, bin => @res.join("") } );
+ }
+ }
+ }
+ }
+ say "Max 1's : {$len}";
+ say "{$_<l>} -> {$_<r>} : {$_<bin>}" for @results;
+}
+
+sub flip( @bin is copy, $l, $r ) {
+ @bin[$l..$r] = @bin[$l..$r].map( { abs($_-1) } );
+ @bin;
+}
+