DEV Community

Discussion on: Daily Challenge #149 - Fun with Lamps

Collapse
 
idanarye profile image
Idan Arye

Rust:

fn lamps(current_states: &[usize]) -> usize {
    let result_for = |offset: usize| {
        current_states.iter().enumerate().filter(|(index, &state)| {
            let desired_state = (index + offset) % 2;
            desired_state == state
        }).count()
    };
    std::cmp::min(result_for(0), result_for(1))
}

fn main() {
    assert_eq!(lamps(&[1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1]), 5);
    assert_eq!(lamps(&[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), 6);
    assert_eq!(lamps(&[1, 0, 1]), 0);
    assert_eq!(lamps(&[1, 0, 1, 0]), 0);
    assert_eq!(lamps(&[0, 1, 0, 1, 0]), 0);
    assert_eq!(lamps(&[1, 0, 1, 0, 0, 1, 0, 1]), 4);
    assert_eq!(lamps(&[1,0,0,1,1,0,0,0,0,1,0]), 5);
}