diff options
| -rw-r--r-- | challenge-342/ash/raku/ch-2.raku | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-342/ash/raku/ch-2.raku b/challenge-342/ash/raku/ch-2.raku new file mode 100644 index 0000000000..555ee7e822 --- /dev/null +++ b/challenge-342/ash/raku/ch-2.raku @@ -0,0 +1,23 @@ +# Task 2 of the Weekly Challenge 342 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-342/#TASK2 + +say max-score('0011'); # 4 +say max-score('0000'); # 3 +say max-score('1111'); # 3 +say max-score('0101'); # 3 +say max-score('011101'); # 5 + +sub max-score($s) { + my $score = 0; + for 1 .. $s.chars - 1 -> $i { + my $left = $s.substr(0, $i); + my $right = $s.substr($i, $s.chars); + + my $score-left = $left.comb('0').elems; + my $score-right = $right.comb('1').elems; + + $score = max($score, $score-left + $score-right); + } + + return $score; +} |
