aboutsummaryrefslogtreecommitdiff
path: root/challenge-115/arne-sommer/raku/string-chain
blob: 1922b29dd291a86a51f3e10e5fd783dd01d54a40 (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
#! /usr/bin/env raku

unit sub MAIN (*@S where @S.elems > 1, :v(:$verbose), :n(:$no-duplicates));

die "Non-unique string not allowed" if $no-duplicates && @S.elems != @S.unique.elems;

for @S.permutations -> @perm
{
  say ": perm: { @perm.join(", ") }" if $verbose;

  if is-circle(@perm)
  {
    say 1;
    exit;
  }
}

say 0;

sub is-circle (@list is copy)
{
  my $first       = @list.shift;
  my $first-start = $first.substr(0,1);
  my $second;

  while @list
  {
    $second = @list.shift;
    return False if $first.substr(*-1,1) ne $second.substr(0,1);
    $first  = $second;
  }
    
  return True if $first-start eq $second.substr(*-1,1);
  return False;
}