diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-09-15 10:28:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-15 10:28:49 +0100 |
| commit | 20478fbeaa50058c0cc7d86d56674928952e5420 (patch) | |
| tree | 2cf09f4c8019db2d81834ca9944d4f7d094f0cff | |
| parent | d33a07de9e844cf604a9c714a93f5d3a125fbbcf (diff) | |
| parent | 63c23820adbd1e4434a9b08f0cb34b2d8c2fb8fd (diff) | |
| download | perlweeklychallenge-club-20478fbeaa50058c0cc7d86d56674928952e5420.tar.gz perlweeklychallenge-club-20478fbeaa50058c0cc7d86d56674928952e5420.tar.bz2 perlweeklychallenge-club-20478fbeaa50058c0cc7d86d56674928952e5420.zip | |
Merge pull request #627 from threadless-screw/ozzy-wk25p6
Perl6 solution to challenge 1 - dogslow, but it works
| -rwxr-xr-x | challenge-025/ozzy/perl6/ch-1.p6 | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-025/ozzy/perl6/ch-1.p6 b/challenge-025/ozzy/perl6/ch-1.p6 new file mode 100755 index 0000000000..2e25e8c5ce --- /dev/null +++ b/challenge-025/ozzy/perl6/ch-1.p6 @@ -0,0 +1,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; +}
\ No newline at end of file |
