From 2c7ee686d1e2a419cda5160287b61a4f86e77e56 Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sat, 14 Sep 2019 21:36:48 -0600 Subject: Joelle's Solutions for 25.1 in Perl 6 & 5. --- challenge-025/joelle-maslak/perl5/ch-1.pl | 88 +++++++++++++++++++++++++++++++ challenge-025/joelle-maslak/perl6/ch-1.p6 | 69 ++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100755 challenge-025/joelle-maslak/perl5/ch-1.pl create mode 100755 challenge-025/joelle-maslak/perl6/ch-1.p6 diff --git a/challenge-025/joelle-maslak/perl5/ch-1.pl b/challenge-025/joelle-maslak/perl5/ch-1.pl new file mode 100755 index 0000000000..92db1edf95 --- /dev/null +++ b/challenge-025/joelle-maslak/perl5/ch-1.pl @@ -0,0 +1,88 @@ +#!/usr/bin/env perl + +use v5.16; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use Parallel::WorkUnit 2.181850; + +my @NAMES = qw( 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 +); +my %NEXTONES; + +for my $name (@NAMES) { + my $last_letter = substr( $name, -1 ); + $NEXTONES{$name} = [ + sort + grep { $_ ne $name } + grep { substr( $_, 0, 1 ) eq $last_letter } @NAMES + ]; +} + +MAIN: { + my (@longest) = build_longest(); + + my $i = 1; + for my $path (@longest) { + say $i++ . " (" . scalar(@$path) . " elements): " . join ' ', $path->@*; + } +} + +sub build_longest() { + my $wu = Parallel::WorkUnit->new(); + + for my $name (@NAMES) { + $wu->async( sub { return build_longest_helper($name) } ); + } + my @results = $wu->waitall(); + + my (@best_paths); + + for my $new_path (@results) { + if ( ( scalar(@best_paths) == 0 ) + or scalar( $new_path->[0]->@* ) > scalar( $best_paths[0]->@* ) ) + { + @best_paths = $new_path->@*; + } elsif ( scalar( $new_path->[0]->@* ) == scalar( $best_paths[0]->@* ) ) { + push @best_paths, $new_path->@*; + } + } + + return @best_paths; +} + +sub build_longest_helper(@path) { + my @best_paths; + $best_paths[0] = \@path; + + my @next_possible = $NEXTONES{ $path[-1] }->@*; + for my $name ( sort @next_possible ) { + next if ( scalar( grep { $_ eq $name } @path ) ); + + my @tmppath = @path; + push @tmppath, $name; + + my $new_paths = build_longest_helper(@tmppath); + + if ( scalar( $new_paths->[0]->@* ) > scalar( $best_paths[0]->@* ) ) { + @best_paths = $new_paths->@*; + } elsif ( scalar( $new_paths->[0]->@* ) == scalar( $best_paths[0]->@* ) ) { + push @best_paths, $new_paths->@*; + } + } + + return \@best_paths; +} + diff --git a/challenge-025/joelle-maslak/perl6/ch-1.p6 b/challenge-025/joelle-maslak/perl6/ch-1.p6 new file mode 100755 index 0000000000..2a35474a0d --- /dev/null +++ b/challenge-025/joelle-maslak/perl6/ch-1.p6 @@ -0,0 +1,69 @@ +#!/usr/bin/env perl6 +use v6; + +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 +»; + +my %NEXTONES; +for @NAMES -> $name { + my $last-letter = $name.comb[*-1]; + %NEXTONES{$name} = @NAMES.grep( { $^a.comb[0] eq $last-letter } ).grep( { $^a ne $name } ).sort.list; +} + +sub MAIN() { + my @longest = build-longest(); + + my $i = 1; + for @longest -> $path { + say "{$i++} ({$path.elems} elements): " ~ $path.map: { $^a.join(" ") }; + } +} + +multi sub build-longest() { + my $c = Channel.new(); + + await @NAMES.map: { + start { $c.send: [ build-longest( @( $_ ) ) ] } + } + $c.close; + + my @best-paths; + for $c.list -> $new-path { + if @best-paths.elems == 0 or $new-path[0].elems > @best-paths[0].elems { + @best-paths = @($new-path); + } elsif $new-path[0].elems == @best-paths[0].elems { + @best-paths.append: @($new-path); + } + } + + return @best-paths; +} + +multi sub build-longest($path) { + my @best-paths = [ $path ]; + + my @next-possible = %NEXTONES{$path[*-1]}.list; + for (@next-possible ∖ @($path)).keys.sort -> $name { + my @tmppath = @($path); + @tmppath.append: $name; + my $new-paths = build-longest(@tmppath); + + if $new-paths[0].elems > @best-paths[0].elems { + @best-paths = @($new-paths); + } elsif $new-paths[0].elems == @best-paths[0].elems { + @best-paths.append: @($new-paths); + } + } + + return @best-paths; +} + + -- cgit