aboutsummaryrefslogtreecommitdiff
path: root/challenge-051/markus-holzer
diff options
context:
space:
mode:
authorMarkus "Holli" Holzer <holli.holzer@gmail.com>2020-03-09 21:29:07 +0100
committerMarkus "Holli" Holzer <holli.holzer@gmail.com>2020-03-09 21:29:07 +0100
commit735f6d0faeb7df7f4f58005daaa64366967f1708 (patch)
tree8d263eef5b0cde4968360d25fb1de804c171ac69 /challenge-051/markus-holzer
parent7dde704d9cbf099f8872e219217580d778a40fc1 (diff)
downloadperlweeklychallenge-club-735f6d0faeb7df7f4f58005daaa64366967f1708.tar.gz
perlweeklychallenge-club-735f6d0faeb7df7f4f58005daaa64366967f1708.tar.bz2
perlweeklychallenge-club-735f6d0faeb7df7f4f58005daaa64366967f1708.zip
this is nicer
Diffstat (limited to 'challenge-051/markus-holzer')
-rw-r--r--challenge-051/markus-holzer/raku/ch-2.p648
1 files changed, 29 insertions, 19 deletions
diff --git a/challenge-051/markus-holzer/raku/ch-2.p6 b/challenge-051/markus-holzer/raku/ch-2.p6
index 70870b59eb..03b149de78 100644
--- a/challenge-051/markus-holzer/raku/ch-2.p6
+++ b/challenge-051/markus-holzer/raku/ch-2.p6
@@ -1,28 +1,47 @@
use Terminal::ANSIColor;
-use Terminal::Table;
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 !$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 is-consecutive( @n ) {
+ not @n.rotor(2 => -1).first( -> ($a, $b) { $a + 1 != $b }) }
+
@n
.pairs
.combinations(1..*)
@@ -31,12 +50,3 @@ sub consecutive-combinations( @n )
;
}
-my sub is-consecutive( @n )
-{
- @n.elems == 1
- or
- not @n.rotor(2 => -1).first({ .[0] + 1 == .[1] })
-}
-
-
-