aboutsummaryrefslogtreecommitdiff
path: root/challenge-025/ozzy/perl6/ch-1.p6
blob: 2e25e8c5cebf1308844c00028b6781cdea1ab78a (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env perl6

# PROBLEM: Find longest chain of Pokemon names wherein each next name starts with the last letter
# of the previous name.
#
# SOLUTION: Test all possible solutions and report every new record. 'Length' is defined as the number
# of chained names, and, subsidiarily, the overall number of characters. It turns out that there are 416
# longest chains (starting with 'machamp') that contain 23 names, with an overall number of characters
# of 174.

my @names = < audino bagon baltoy banette bidoof braviary bronzor carracosta charmeleon cresselia croagunk
              darmanitan deino emboar emolga exeggcute gabite girafarig gulpin haxorus heatmor heatran
              ivysaur jellicent jumpluff kangaskhan kricketune landorus ledyba loudred lumineon lunatone
              machamp magnezone mamoswine nosepass petilil pidgeotto pikachu pinsir poliwrath poochyena
              porygon2 porygonz registeel relicanth remoraid rufflet sableye scolipede scrafty seaking
              sealeo silcoon simisear snivy snorlax spoink starly tirtouga trapinch treecko tyrogue
              vigoroth vulpix wailord wartortle whismur wingull yamask >.sort;

my $max_seq_count = 0;
my $max_length    = 0;


sub search_next (@name, @names_left) {

    for @names_left.kv -> $idx_cand, $name_cand {

         next unless $name_cand.defined;

         my $f = $name_cand.comb[0];
         my $l = @name[*-1].comb[*-1];

         last if $f gt $l;
         next if $f lt $l;

         my @name_new = |@name, $name_cand;

         # New record length ?
         if @name_new.elems >= $max_seq_count {
             if @name_new.elems > $max_seq_count {
                 $max_seq_count = @name_new.elems;
                 $max_length = join('', @name_new).chars;
                 say $max_seq_count, ',', $max_length, ' -> ', @name_new;
             } else {
                 my $len = join('', @name_new).chars;
                 if $len >= $max_length {
                     $max_length = $len;
                     say $max_seq_count, ',', $max_length, ' -> ', @name_new;
                 }
             }
         }
         splice (my @names_left_new = @names_left), $idx_cand, 1, Nil;
         search_next @name_new, @names_left_new;
    }
}

for @names.kv -> $idx, $name {
    splice (my @names_left = @names), $idx, 1, Nil;
    search_next (my @name = $name), @names_left;
}