aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-09-14 21:36:48 -0600
committerJoelle Maslak <jmaslak@antelope.net>2019-09-14 21:36:48 -0600
commit2c7ee686d1e2a419cda5160287b61a4f86e77e56 (patch)
treea6f56879e8355da0484a0837a30eb992164da3a7
parente4e13916c4d132bd6f64695ad7794c1051427704 (diff)
downloadperlweeklychallenge-club-2c7ee686d1e2a419cda5160287b61a4f86e77e56.tar.gz
perlweeklychallenge-club-2c7ee686d1e2a419cda5160287b61a4f86e77e56.tar.bz2
perlweeklychallenge-club-2c7ee686d1e2a419cda5160287b61a4f86e77e56.zip
Joelle's Solutions for 25.1 in Perl 6 & 5.
-rwxr-xr-xchallenge-025/joelle-maslak/perl5/ch-1.pl88
-rwxr-xr-xchallenge-025/joelle-maslak/perl6/ch-1.p669
2 files changed, 157 insertions, 0 deletions
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;
+}
+
+