aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/barroff/raku/ch-1.p6
blob: e4beb0f2b4749f142d7f0c9add1f38088fd2f731 (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
#!/usr/bin/env raku

use v6.d;

sub special-positions(@ints --> Int) {
    my @special-rows = grep({ sum(@ints[$_]) == 1 }, 0..@ints.elems - 1);
    my &special-columns = sub ($i) {
        my $column = @ints[$i].first: * == 1, :k;
        sum(map({ @ints[$_][$column]}, 0..@ints.elems - 1));
    }
    grep({ &special-columns($_) == 1 }, @special-rows).elems;
}

#| Run test cases
multi sub MAIN('test') {
    use Test;
    plan 2;

    is special-positions([ [1, 0, 0],
                           [0, 0, 1],
                           [1, 0, 0],
                         ]), 1, 'works for first matrix';
    is special-positions([ [1, 0, 0],
                           [0, 1, 0],
                           [0, 0, 1],
                         ]), 3, 'works for second matrix';
}