/
ch-2.raku
executable file
·55 lines (54 loc) · 1.53 KB
/
ch-2.raku
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
43
44
45
46
47
48
49
50
51
52
53
54
55
use v6 ;
#concatenate by gluing the digits together to a string and by converting
#them to an int afterwards
sub concatenate( $aSequence ) {
my $total ;
my $len = $aSequence.elems ;
for (0..$len - 1 ) -> $num {
$total ~= $aSequence[ $num ].Str ;
}
return $total.Int ;
}
say "Enter some integers greater than 0 , separated by blanks!" ;
my $line = $*IN.get ;
my @numbers = $line.words.map( {.Int} ) ;
my @permuted ; #for the concatenated permutations
my @permutations = @numbers.permutations( ) ;
for @permutations -> $permu {
@permuted.push( concatenate( $permu ) ) ;
}
my @selected = @permuted.grep( { $_ %% 3 } ) ;
if ( @selected.elems > 0 ) {
@selected .= sort ; #sort in ascending order
say @selected[*-1] ; #and select the largest one
}
else {#create combinations of decreasing length and permutate each
#combination. Stop when a number divisible by 3 has been found
my $len = @numbers.elems ;
my $i = $len ;
@permuted = ( ) ;#clear the array we've used before
while ( $i != 1 ) {
my @combinations = @numbers.combinations( $i ) ;
for @combinations -> $combi {
my @permu = $combi.permutations( ) ;
for @permu -> $aPermu {
my $concat = concatenate( $aPermu ) ;
@permuted.push( $concat ) ;
}
}
@selected = @permuted.grep( { $_ %% 3 } ) ;
if ( @selected ) {
@selected .= sort ;
last ;
}
else {
$i-- ;
}
}
if ( @selected ) {
say @selected[*-1] ;
}
else {
say "-1" ;
}
}