aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-074/luca-ferrari/raku/ch-2.p641
1 files changed, 20 insertions, 21 deletions
diff --git a/challenge-074/luca-ferrari/raku/ch-2.p6 b/challenge-074/luca-ferrari/raku/ch-2.p6
index 78559cd9b0..2c0da8f0ea 100644
--- a/challenge-074/luca-ferrari/raku/ch-2.p6
+++ b/challenge-074/luca-ferrari/raku/ch-2.p6
@@ -2,33 +2,32 @@
sub MAIN( Str $S where { $S.chars > 2 } ) {
- my $fnr = Nil;
my @result;
my %counting;
my @chars = $S.comb( '', :skip-empty );
- for 0 ..^ @chars.elems {
- my $current-char = @chars[ $_ ];
-
- if ! $fnr {
- $fnr = $current-char;
- }
- elsif $current-char ~~ $fnr {
- my %counting;
- %counting{ $_ }++ for @chars[ 0 .. $_ ];
- $fnr = %counting.pairs.grep( { .value == 1 && .key !~~ $current-char } ).first.key // '#';
- "selezionato $fnr".say;
- }
- elsif $_ !~~ $fnr {
- say "$fnr $current-char";
- $fnr = $current-char;
- }
- else {
- $fnr = '#';
- }
+ for 0 ..^ @chars.elems -> $index {
+ my $current-char = @chars[ $index ];
- @result.push: $fnr;
+
+ # if the result array is empty
+ # or the char is not in the array, it is
+ # ok to push
+ @result.push( $current-char ) && next if ! @result || ! @result.grep( * ~~ $current-char );
+
+
+ # if here I need to search for the first rightmost
+ # not repeating char so far
+ %counting = %();
+ %counting{ $_ }++ for $S.substr( 0 .. $index ).comb( '', :skip-empty );
+ my $fnr = $S.substr( 0 .. $index )
+ .comb( '', :skip-empty )
+ .reverse
+ .grep( { %counting{ $_ }:exists && %counting{ $_ } == 1 } )
+ .first // '#';
+
+ @result.push: $fnr;
}
@result.join.say;