forked from manwar/perlweeklychallenge-club
/
ch-2.raku
executable file
·51 lines (45 loc) · 1.04 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
#!/usr/bin/env raku
use v6;
sub weakestRows(@matrix) {
my @oneCount = @matrix.map({ $_.sum });
my @weakest = (0 .. @oneCount.end).sort: {
# sort by count first
@oneCount[$^a] <=> @oneCount[$^b]
||
# then by index order
$^a <=> $^b
};
return @weakest;
}
sub formatMatrix(@matrix, $indent) {
my @output;
for @matrix -> @row {
my $output_row = q{ } x $indent ~ " [";
$output_row ~= @row.join(', ') ~ "]";
@output.push($output_row);
}
return "[\n"
~ @output.join(",\n")
~ "\n"
~ q{ } x $indent ~ "]";
}
sub solution(@matrix) {
say 'Input: $matrix = ' ~ formatMatrix(@matrix, 17);
my @output = weakestRows(@matrix);
say 'Output: (' ~ @output.join(', ') ~ ')';
}
say "Example 1:";
solution([
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 0],
[1, 0, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 1, 1, 1, 1]
]);
say "\nExample 2:";
solution([
[1, 0, 0, 0],
[1, 1, 1, 1],
[1, 0, 0, 0],
[1, 0, 0, 0]
]);