diff options
| author | boblied <boblied@gmail.com> | 2023-03-07 10:33:16 -0600 |
|---|---|---|
| committer | boblied <boblied@gmail.com> | 2023-03-08 18:40:40 -0600 |
| commit | 39222b30c78b2727fa5786ed3205fdf50ee758b1 (patch) | |
| tree | d4bfb977703717c4caed17df23047f6c067221ce | |
| parent | 8f8efeb2bd1142342d75988e3fd69faa1102810a (diff) | |
| download | perlweeklychallenge-club-39222b30c78b2727fa5786ed3205fdf50ee758b1.tar.gz perlweeklychallenge-club-39222b30c78b2727fa5786ed3205fdf50ee758b1.tar.bz2 perlweeklychallenge-club-39222b30c78b2727fa5786ed3205fdf50ee758b1.zip | |
Week 176 Task 1
| -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; } |
