aboutsummaryrefslogtreecommitdiff
path: root/challenge-337/benjamin-andre/rust/ch-2.rs
blob: 115575be0e18e06e77c5bb62e96f7dfa259513fb (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
#!/bin/sh
//usr/bin/env rustc --test $0 -o kachow && ./kachow --nocapture; rm -f kachow ; exit

fn odd_matrix(row: usize, col: usize, locations: &[[usize; 2]]) -> i32 {
    let mut row_counts = vec![0; row];
    let mut col_counts = vec![0; col];
    for &[r, c] in locations {
        row_counts[r] += 1;
        col_counts[c] += 1;
    }
    (0..row)
        .map(|r| {
            (0..col)
                .filter(|&c| (row_counts[r] + col_counts[c]) % 2 == 1)
                .count()
        })
        .sum::<usize>() as i32
}

#[test]
fn example() {
    assert_eq!(odd_matrix(2, 3, &[[0, 1], [1, 1]]), 6);
    assert_eq!(odd_matrix(2, 2, &[[1, 1], [0, 0]]), 0);
    assert_eq!(odd_matrix(3, 3, &[[0, 0], [1, 2], [2, 1]]), 0);
    assert_eq!(odd_matrix(1, 5, &[[0, 2], [0, 4]]), 2);
    assert_eq!(odd_matrix(4, 2, &[[1, 0], [3, 1], [2, 0], [0, 1]]), 8);
}