From beece1a136152e7819ab4a8ff7f962df078cbd14 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 10 Jun 2019 09:59:27 +0100 Subject: Longest shared path --- challenge-012/simon-proctor/perl6/ch-2.p6 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100755 challenge-012/simon-proctor/perl6/ch-2.p6 diff --git a/challenge-012/simon-proctor/perl6/ch-2.p6 b/challenge-012/simon-proctor/perl6/ch-2.p6 new file mode 100755 index 0000000000..cbfd7b1c73 --- /dev/null +++ b/challenge-012/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,20 @@ +#!/usr/bin/perl6 + +use v6; + +my %*SUB-MAIN-OPTS = :named-anywhere; + +#| Given a list of paths returns the longest shared path between them +multi sub MAIN( + *@paths, #= List of paths to check + :$seperator = $*SPEC.dir-sep #= Optional directory seperator. Defaults to the system standard. +) { + my @checks = @paths.sort( { $^b.codes <=> $^a.codes } )[0].split($seperator)[1..*-2]; + my $path = $seperator; + my $new-path = $path; + while ( all( @paths ) ~~ / ^ $new-path / ) { + $path = $new-path; + $new-path ~= ( @checks.shift ~ $seperator ); + }; + say $path; +} -- cgit From 295001f32ec6fd9ef204c62b86af10ae50945597 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 10 Jun 2019 10:01:30 +0100 Subject: Fixed empty call bug --- challenge-012/simon-proctor/perl6/ch-2.p6 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/challenge-012/simon-proctor/perl6/ch-2.p6 b/challenge-012/simon-proctor/perl6/ch-2.p6 index cbfd7b1c73..22170940f4 100755 --- a/challenge-012/simon-proctor/perl6/ch-2.p6 +++ b/challenge-012/simon-proctor/perl6/ch-2.p6 @@ -4,6 +4,10 @@ use v6; my %*SUB-MAIN-OPTS = :named-anywhere; +multi sub MAIN is hidden-from-USAGE { + say $*USAGE; +} + #| Given a list of paths returns the longest shared path between them multi sub MAIN( *@paths, #= List of paths to check -- cgit