aboutsummaryrefslogtreecommitdiff
path: root/challenge-051/markus-holzer/ch-2.p6
blob: 70870b59ebbd7f749165ab1ffc30a18195b31a52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
	{
		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; 
	}
}


sub consecutive-combinations( @n )
{
	my sub keys-of( @p )   { @p.map: *.key   }
	my sub values-of( @p ) { @p.map: *.value }

	@n
		.pairs
		.combinations(1..*)
		.grep({ is-consecutive( .&keys-of ) })
		.map({ .&values-of })
	;
}

my sub is-consecutive( @n ) 
{ 
	@n.elems == 1 
		or 
	not @n.rotor(2 => -1).first({ .[0] + 1 == .[1] }) 
}