diff options
| -rw-r--r-- | challenge-074/mark-anderson/raku/ch-2.raku | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/challenge-074/mark-anderson/raku/ch-2.raku b/challenge-074/mark-anderson/raku/ch-2.raku index 2447d5f9a5..c572e643c3 100644 --- a/challenge-074/mark-anderson/raku/ch-2.raku +++ b/challenge-074/mark-anderson/raku/ch-2.raku @@ -1,25 +1,31 @@ -unit sub MAIN($string); +sub MAIN($string) { + my @fnr-chars = gather { + for 1..$string.chars -> $i { + take fnr($string.substr(0, $i)); + } + } + + say @fnr-chars.join; +} -my %seen; -my $singles; +sub fnr($string) { + state %seen; + state $singles; -my @first-non-repeatings = gather { - for 1..$string.chars -> $i { - my $letter = $string.substr($i-1, 1); + my $letter = $string.substr($string.chars-1, 1); - %seen{$letter}++; + %seen{$letter}++; - if %seen{$letter} == 1 { + given %seen{$letter} { + when 1 { $singles ~= $letter; - take $letter; + return $letter; } - - else { + when 2 { my $idx = $singles.index($letter); $singles = $singles.substr(0, $idx) ~ $singles.substr($idx+1); - take $singles.substr($singles.chars-1, 1) // "#"; } } -} -say @first-non-repeatings.join; + return $singles.substr($singles.chars-1, 1) // "#"; +} |
