aboutsummaryrefslogtreecommitdiff
path: root/challenge-070/javier-luque/raku/ch-2.p6
blob: 745f2f2f8c238a828b79b34ee4ddd406d20d549b (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
# Test: perl6 ch-2.p6
sub MAIN() {
	say gray-code(4);
}

sub gray-code(Int $N is copy where $N >= 2 && $N <=5 ) {
	my @S1 = ('00','01','11','10');

	while ($N > 2) {
		# Flip the array
		my @S2 = @S1.reverse;

		# Prefix
		@S1 = @S1.map({ '0' ~ $_ });
		@S2 = @S2.map({ '1' ~ $_ });

		# Concatenate
		@S1 = flat @S1, @S2;

		$N--;
	}

	# Convert to decimal
	@S1 = @S1.map({ "0b$_".Int });

	return @S1.perl;
}