DEV Community

loading...

Discussion on: Daily Challenge #274 - Aerial Firefighting

Collapse
jrop profile image
Jonathan Apodaca

Rust:

fn water_bombs(spec: &str, width: usize) -> usize {
    let result = spec.split("Y") // split into sections
        // for each section, see how many bombs it takes to handle:
        .map(|xs| ((xs.len() as f64) / (width as f64)).ceil().trunc() as usize)
        // sum:
        .fold(0, |acc, n| acc + n);
    result
}
fn main() {
   assert_eq!(water_bombs("xxYxx", 2), 2);
   assert_eq!(water_bombs("xxYxx", 1), 4);
   assert_eq!(water_bombs("xxxxYxYx", 5), 3);
   assert_eq!(water_bombs("xxxxYxYx", 2), 4);
   assert_eq!(water_bombs("xxxxxYxYx", 2), 5);
   assert_eq!(water_bombs("xxxxYxYx", 4), 3);
   assert_eq!(water_bombs("xxYxx", 3), 2);
}
Forem Open with the Forem app