diff options
| -rw-r--r-- | challenge-176/bob-lied/perl/ch-1.pl | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/challenge-176/bob-lied/perl/ch-1.pl b/challenge-176/bob-lied/perl/ch-1.pl index 1aaaeb52db..2c663c5d77 100644 --- a/challenge-176/bob-lied/perl/ch-1.pl +++ b/challenge-176/bob-lied/perl/ch-1.pl @@ -15,18 +15,58 @@ use v5.36; +use builtin qw/true false/; +no warnings "experimental::builtin"; + +use List::Util qw/all/; + use Getopt::Long; -my $Verbose = 0; -my $DoTest = 0; +my $DoTest = false; +my $DoAll = false; +my $Size = 6; -GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +GetOptions("test" => \$DoTest, "all" => \$DoAll, "size:i" => \$Size); exit(!runTest()) if $DoTest; +sub isPermutation($digits, $m) +{ + my @m = sort split("", $m); + return false if $#m != $#{$digits}; + return all { $digits->[$_] == $m[$_] } 0 .. $#m; +} + +say join(" ", smallestPermuted($Size, $DoAll)->@*); + +sub smallestPermuted($size = 6, $doAll = false) +{ + my @result; + MAG: for ( my $magnitude = 100 ; $magnitude <= 10**$size ; $magnitude *= 10 ) + { + my $max = int( $magnitude / 6 ); + my $base = int ( $max / 10 ); + for ( my $i = $base + 1 ; $i <= $base + $max ; $i++ ) + { + my @digits = sort split("", $i); + + if ( all { isPermutation(\@digits, $i * $_) } 2 .. 6 ) + { + push @result, $i; + last MAG unless $doAll; + } + } + } + return \@result; +} + sub runTest { use Test2::V0; - is(0, 1, "FAIL"); + is( smallestPermuted(2), [], "2 digits"); + is( smallestPermuted(3), [], "3 digits"); + is( smallestPermuted(4), [], "4 digits"); + is( smallestPermuted(5), [], "5 digits"); + is( smallestPermuted(6), [ 142857 ], "6 digits"); done_testing; } |
