aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/arne-sommer/raku/unique-subsequence
blob: 9cd85cf00d22b8618e18016e29e94354f684ae50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env raku

unit sub MAIN (Str $S where $S.chars > 0,
               Str $T where $T.chars > 0,
	       :v(:$verbose));

my $S-length = $S.chars;
my $T-length = $T.chars;
my $binary   = '1' x $S-length;
my $max      = $binary.parse-base(2);
my $matches  = 0;

for 1 .. $max -> $current
{
  my $mask = $current.fmt('%0' ~ $S-length ~ 'b');

  if $mask.comb.sum != $T-length
  {
    say ":   Skipped binary mask '{ $mask }' - wrong number of 1s" if $verbose;
    next;
  }

  my $candidate = (^$S-length).map({ $mask.substr($_, 1) eq '1' ?? $S.substr($_,1) !! '' }).join;

  if $candidate eq $T
  {
    $matches++;
    say ": + Match found with binary mask '{ $mask }'." if $verbose;
  }
  else
  {
    say ":   Considering binary mask '{ $mask}' - no match" if $verbose;
  }
}

say $matches;