diff options
| -rw-r--r-- | challenge-025/simon-proctor/perl6/ch-1.p6 | 61 | ||||
| -rw-r--r-- | challenge-025/simon-proctor/pokemon.txt | 70 | ||||
| -rw-r--r-- | challenge-025/simon-proctor/test.txt | 1 |
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-025/simon-proctor/perl6/ch-1.p6 b/challenge-025/simon-proctor/perl6/ch-1.p6 new file mode 100644 index 0000000000..cc0dd7aa48 --- /dev/null +++ b/challenge-025/simon-proctor/perl6/ch-1.p6 @@ -0,0 +1,61 @@ +#!/usr/bin/env perl6 + +use v6; + +role NodeLink { + has Str $.target-id; + has Str $.value; +} + +role Node { + has NodeLink %.links; + has Str $.id; + + method link-count { @.links.elems } + + method add-link( NodeLink $link ) { + %.links{$link.value} = $link; + } + + method targets { + my %out; + for %.links.values -> $link { + %out{$link.target-id} //= 0; + %out{$link.target-id}++; + } + return %out; + } + + method remove-link( Str $target ) { + my $key = %.links.values.first( { $_.target-id ~~ $target } ).value; + %.links{$key}:delete; + } +} + + + +subset FileExists of Str where { $_.IO.e && $_.IO.f }; + +#| Given a list of words calculate the longest chain of words where the last letter of one is the same as first letter of the next. +multi sub MAIN( + FileExists $file #= Path to file of words +) { + my %nodes; + for $file.IO.words -> $word { + my $node-key = $word.substr(0,1); + %nodes{$node-key} //= Node.new( :id($node-key) ); + my $link-to = $word.substr($word.codes-1,1); + %nodes{$node-key}.add-link( NodeLink.new( :target-id($link-to), :value($word) ) ); + } + my $current = %nodes.values.sort( { $^b.link-count <=> $^a.link-count } )[0]; + + while $current && $current.link-count > 0 { + my %targets = $current.targets; + my $next = %nodes.values.sort( { $^b.link-count <=> $^a.link-count } ).first( { %targets{$_.id}:exists } ); + if $next { + my $link = $current.remove-link( $next.id ); + say $link.value; + } + $current = $next; + } +} diff --git a/challenge-025/simon-proctor/pokemon.txt b/challenge-025/simon-proctor/pokemon.txt new file mode 100644 index 0000000000..1f1f384ce7 --- /dev/null +++ b/challenge-025/simon-proctor/pokemon.txt @@ -0,0 +1,70 @@ +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 diff --git a/challenge-025/simon-proctor/test.txt b/challenge-025/simon-proctor/test.txt new file mode 100644 index 0000000000..c27ecf548a --- /dev/null +++ b/challenge-025/simon-proctor/test.txt @@ -0,0 +1 @@ +add acid dire era earned |
