aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-051/markus-holzer/raku/ch-2.p653
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