diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-01-13 18:44:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-13 18:44:42 +0000 |
| commit | 7fba4c7f886d6bcdccc83b6c5387af79c834f71e (patch) | |
| tree | 0983bbef2a6b1674ef2e32a358dca1fc347fa780 | |
| parent | 90d543703e429943ca739b6470a9d4092205928d (diff) | |
| parent | c597c4bba4a22d1c2b637d983ad12f9911ee0ae6 (diff) | |
| download | perlweeklychallenge-club-7fba4c7f886d6bcdccc83b6c5387af79c834f71e.tar.gz perlweeklychallenge-club-7fba4c7f886d6bcdccc83b6c5387af79c834f71e.tar.bz2 perlweeklychallenge-club-7fba4c7f886d6bcdccc83b6c5387af79c834f71e.zip | |
Merge pull request #1134 from holli-holzer/master
Solutions Markus Holzer
| -rw-r--r-- | challenge-043/markus-holzer/perl6/ch-1.p6 | 43 | ||||
| -rw-r--r-- | challenge-043/markus-holzer/perl6/ch-2.p6 | 61 |
2 files changed, 104 insertions, 0 deletions
diff --git a/challenge-043/markus-holzer/perl6/ch-1.p6 b/challenge-043/markus-holzer/perl6/ch-1.p6 new file mode 100644 index 0000000000..9858a1dd12 --- /dev/null +++ b/challenge-043/markus-holzer/perl6/ch-1.p6 @@ -0,0 +1,43 @@ +use Test; +plan 6; + +my $total = 11; +my @gap-v = 1, 2, 3, 4, 6; +my @rings = + { :color('Red'), :v(9), :n(0) }, + { :color('Green'), :v(5) }, + { :color('Black') }, + { :color('Yellow'), :v(7) }, + { :color('Blue'), :v(8), :m(0) }; + +@rings[0]<m> = @rings[1]<n> = find-x( @rings[0]<n>, @rings[0]<v> ); +@rings[4]<n> = @rings[3]<m> = find-x( @rings[4]<m>, @rings[4]<v> ); +@rings[1]<m> = @rings[2]<n> = find-x( @rings[1]<n>, @rings[1]<v> ); +@rings[3]<n> = @rings[2]<m> = find-x( @rings[3]<m>, @rings[3]<v> ); +@rings[2]<v> = find-x( @rings[2]<n>, @rings[2]<m> ); + +for @rings -> $ring +{ + ok $ring<m> + $ring<n> + $ring<v> == $total, "$ring<color> ring sum ok"; +} + +ok @gap-v.elems == 0, "all gap values in m processed"; + +say "Black value: @rings[2]<v>"; + +sub find-x( $gap, $v ) +{ + my $j = $total - $v - $gap; + my $i = @gap-v.first({ $_ == $j }, :k); + die "Can't find index ($gap, $v)" unless $i.defined; + @gap-v.splice( $i, 1 )[0]; +} + +dd @rings; + +# Array @rings = [ +# {:color("Red"), :m(2), :n(0), :v(9)}, +# {:color("Green"), :m(4), :n(2), :v(5)}, +# {:color("Black"), :m(1), :n(4), :v(6)}, +# {:color("Yellow"), :m(3), :n(1), :v(7)}, +# {:color("Blue"), :m(0), :n(3), :v(8)} ]
\ No newline at end of file diff --git a/challenge-043/markus-holzer/perl6/ch-2.p6 b/challenge-043/markus-holzer/perl6/ch-2.p6 new file mode 100644 index 0000000000..11b4b2a19c --- /dev/null +++ b/challenge-043/markus-holzer/perl6/ch-2.p6 @@ -0,0 +1,61 @@ +use Test; + +# all self-descriptive numbers +# - must be at least base digits long +# - have digit sums equal to their base, +# - are multiples of that base +# - each digit d at position n counts how many instances of digit n are in m + +multi sub MAIN( Int $base where $_ < 37 ) +{ + .base( $base ).say for + self-descriptive-candidates( $base ) + .grep({ is-self-descriptive( $_, $base ) }); +} + + +multi sub MAIN( "test" ) +{ + ok base-start( 2 ) == 2; + ok base-start( 10 ) == 1000000000; + ok base-start( 16 ) == 0x1000000000000000; + + # test values from Wikipedia + ok is-self-descriptive( parse-base('21200',5), 5 ); + ok is-self-descriptive( 0xC210000000001000, 16 ); + ok is-self-descriptive( 6210001000, 10 ); + ok !is-self-descriptive( 3210001000, 10 ); + ok self-descriptive-candidates(4).first({ is-self-descriptive( $_, 4) }).base(4) eq "1210"; + ok self-descriptive-candidates(5).first({ is-self-descriptive($_, 5) }).base(5) eq "21200"; + ok self-descriptive-candidates(7).first({ is-self-descriptive($_, 7) }).base(7) eq "3211000"; +} + + +sub is-self-descriptive( $number, $base ) +{ + state @digits = (0 .. 9).Array.append( ('A' .. 'Z').Array ); + + my $base-str = $number.base( $base ); + + !so $base-str.comb.pairs.first( -> $p + { + my $digit = @digits[ $p.key ]; + my $count-is = ( $base-str ~~ m:g/ ($digit) / ).elems; + $count-is != parse-base( $p.value.Str, $base ); + }); +} + + +sub self-descriptive-candidates( $base ) +{ + my $base-start = base-start($base); + return $base-start, $base-start + $base, { $_ + $base } ...^ $base-start * $base; +} + +sub base-start( $base ) +{ + my $zeroes = $base - 1; + my $n = "1" ~ ( "0" x $zeroes ); + parse-base( $n, $base ); +} + |
