diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-03-09 21:14:43 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-09 21:14:43 +0000 |
| commit | 34f42ccefb232b2c03ecef0959c8fdea9f368cd2 (patch) | |
| tree | adbbfd034c31f121468dffa5f43063a6e5d2f908 | |
| parent | 993948aac2ccd510823a8131148531500684faff (diff) | |
| parent | 229b8dae509eecd37df5471182b57b5581bd0a64 (diff) | |
| download | perlweeklychallenge-club-34f42ccefb232b2c03ecef0959c8fdea9f368cd2.tar.gz perlweeklychallenge-club-34f42ccefb232b2c03ecef0959c8fdea9f368cd2.tar.bz2 perlweeklychallenge-club-34f42ccefb232b2c03ecef0959c8fdea9f368cd2.zip | |
Merge pull request #1385 from holli-holzer/master
Longer, but easier to understand
| -rw-r--r-- | challenge-051/markus-holzer/raku/ch-2.p6 | 53 |
1 files changed, 33 insertions, 20 deletions
diff --git a/challenge-051/markus-holzer/raku/ch-2.p6 b/challenge-051/markus-holzer/raku/ch-2.p6 index e8b4096159..ced8f024ed 100644 --- a/challenge-051/markus-holzer/raku/ch-2.p6 +++ b/challenge-051/markus-holzer/raku/ch-2.p6 @@ -2,37 +2,50 @@ use Terminal::ANSIColor; sub MAIN( Int $from, Int $to, Bool :$only = False ) { - my @colors = <red green yellow blue magenta cyan>; - print "$_," for gather for $from..$to -> $n + print "$_, " for gather for $from..$to -> $n { - my @subsets = consecutive-combinations( $n.comb ); - my @products = @subsets.map({ [*] $_ }); - my $colorful = @products.elems == @products.unique.elems; - my $color = $colorful ?? @colors.pick !! 'white'; - - take color($color) ~ $n ~ color('reset') - if $colorful or !$only; - }; + my $is-colorful = is-colorful( $n ); + take colorize( $n, $is-colorful ) + if $is-colorful or not $only; + } } +sub colorize( $n, $colorful ) +{ + state @colors = <red green yellow blue magenta cyan>; + my $color = $colorful ?? @colors.pick !! 'white'; + color( $color ) ~ $n ~ color( 'reset' ); +} + +sub is-colorful( $n ) +{ + my @digits = $n.comb; + + # save some work + return False + if ( @digits.repeated ) + or ( $n > 1 && '1' (elem) @digits ); + + not consecutive-combinations( @digits ) + .map({ [*] $_ }) + .repeated + ; +} + sub consecutive-combinations( @n ) { - my sub keys-of( @p ) { @p.map: *.key } - my sub values-of( @p ) { @p.map: *.value } + my sub keys-of( @p ) { @p.map: *.key } + my sub values-of( @p ) { @p.map: *.value } - my multi sub is-consecutive( $a, $b ) { $a + 1 == $b } - my multi sub is-consecutive( @n ) { @n.elems == 1 or [[&is-consecutive]] @n } + my sub is-consecutive( @n ) { + not @n.rotor( 2 => -1 ).first( -> ($a, $b) { $a + 1 != $b }) } @n .pairs - .combinations(1..*) + .combinations( 1..* ) .grep({ is-consecutive( .&keys-of ) }) .map({ .&values-of }) ; -} - - - - +}
\ No newline at end of file |
