fn step(octopi: &mut Vec2d<u8>, verbose: bool) -> usize {
    let mut flashes = 0;
    let mut to_flash = Vec::<(usize, usize)>::with_capacity(octopi.len());
    for row in 0..octopi.nrows() {
        for col in 0..octopi.ncols() {
            octopi[(row, col)] += 1;
            if octopi[(row, col)] > 9 {
                to_flash.push((row, col));
                octopi[(row, col)] = 0;
                flashes += 1;
            }
        }
    }
    while let Some((row, col)) = to_flash.pop() {
        if verbose {
            println!("({}, {}) FLASH", row, col);
        }
        enumerate_adjacent(octopi, (row, col)).for_each(|(row, col)| {
            if let Some(energy) = octopi.at_mut((row, col)) {
                if *energy != 0 {
                    if verbose {
                        println!("  ({}, {}) bumping {} => {}", row, col, *energy, *energy + 1);
                    }
                    *energy += 1;
                    if *energy > 9 {
                        if verbose {
                            println!("  queueing flash on ({}, {})", row, col);
                        }
                        to_flash.push((row, col));
                        *energy = 0;
                        flashes += 1;
                    }
                }
            }
        })
    }
    flashes
}
// Simulate a step and return the number of flashes.
Simulate a step and return the number of flashes.
Simulate a step and return the number of flashes .
fn step(octopi: &mut Vec2d<u8>, verbose: bool) -> usize {
    let mut flashes = 0;
    let mut to_flash = Vec::<(usize, usize)>::with_capacity(octopi.len());
    for row in 0..octopi.nrows() {
        for col in 0..octopi.ncols() {
            octopi[(row, col)] += 1;
            if octopi[(row, col)] > 9 {
                to_flash.push((row, col));
                octopi[(row, col)] = 0;
                flashes += 1;
            }
        }
    }
    while let Some((row, col)) = to_flash.pop() {
        if verbose {
            println!("({}, {}) FLASH", row, col);
        }
        enumerate_adjacent(octopi, (row, col)).for_each(|(row, col)| {
            if let Some(energy) = octopi.at_mut((row, col)) {
                if *energy != 0 {
                    if verbose {
                        println!("  ({}, {}) bumping {} => {}", row, col, *energy, *energy + 1);
                    }
                    *energy += 1;
                    if *energy > 9 {
                        if verbose {
                            println!("  queueing flash on ({}, {})", row, col);
                        }
                        to_flash.push((row, col));
                        *energy = 0;
                        flashes += 1;
                    }
                }
            }
        })
    }
    flashes
}
fn step ( octopi : & mut Vec2d < u8 > , verbose : bool ) -> usize { let mut flashes = 0 ; let mut to_flash = Vec :: < ( usize , usize ) > :: with_capacity ( octopi . len ( ) ) ; for row in 0 .. octopi . nrows ( ) { for col in 0 .. octopi . ncols ( ) { octopi [ ( row , col ) ] += 1 ; if octopi [ ( row , col ) ] > 9 { to_flash . push ( ( row , col ) ) ; octopi [ ( row , col ) ] = 0 ; flashes += 1 ; } } } while let Some ( ( row , col ) ) = to_flash . pop ( ) { if verbose { println ! ( " " row col ) ; } enumerate_adjacent ( octopi , ( row , col ) ) . for_each ( | ( row , col ) | { if let Some ( energy ) = octopi . at_mut ( ( row , col ) ) { if * energy != 0 { if verbose { println ! ( " " row col energy energy 1 ) ; } * energy += 1 ; if * energy > 9 { if verbose { println ! ( " " row col ) ; } to_flash . push ( ( row , col ) ) ; * energy = 0 ; flashes += 1 ; } } } } ) } flashes }
Simulate a step and return the number of flashes.
Simulate a step and return the number of flashes .
octopi
&mut Vec2d<u8>
verbose
bool