Skip to content

Commit

Permalink
Add problem 2257: Count Unguarded Cells in the Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 23, 2024
1 parent 3f5b765 commit 6762fef
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,7 @@ pub mod problem_2250_count_number_of_rectangles_containing_each_point;
pub mod problem_2251_number_of_flowers_in_full_bloom;
pub mod problem_2255_count_prefixes_of_a_given_string;
pub mod problem_2256_minimum_average_difference;
pub mod problem_2257_count_unguarded_cells_in_the_grid;
pub mod problem_2258_escape_the_spreading_fire;
pub mod problem_2259_remove_digit_from_number_to_maximize_result;
pub mod problem_2260_minimum_consecutive_cards_to_pick_up;
Expand Down
76 changes: 76 additions & 0 deletions src/problem_2257_count_unguarded_cells_in_the_grid/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn count_unguarded(m: i32, n: i32, guards: Vec<Vec<i32>>, walls: Vec<Vec<i32>>) -> i32 {
let m = m as u32 as usize;
let n = n as u32 as usize;
let mut grid = vec![0_u8; n * m].into_boxed_slice();

for (c, positions) in [(b'g', guards), (b'w', walls)] {
for position in positions {
let [y, x] = <[_; 2]>::map(position.try_into().ok().unwrap(), |x| x as u32 as usize);

grid[n * y + x] = c;
}
}

let mut guarded = vec![false; n].into_boxed_slice();

grid.chunks_exact_mut(n).for_each(|row| {
let mut left_guarded = false;

row.iter_mut().zip(&mut *guarded).for_each(|(cell, top_guarded)| {
if *cell == 0 {
*cell = u8::from(left_guarded || *top_guarded);
} else {
let guarded = *cell == b'g';

left_guarded = guarded;
*top_guarded = guarded;
}
});
});

guarded.fill(false);

let mut result = 0;

grid.chunks_exact(n).rev().for_each(|row| {
let mut right_guarded = false;

row.iter()
.zip(&mut *guarded)
.rev()
.for_each(|(&cell, bottom_guarded)| match cell {
0 => result += i32::from(!(right_guarded || *bottom_guarded)),
1 => {}
_ => {
let guarded = cell == b'g';

right_guarded = guarded;
*bottom_guarded = guarded;
}
});
});

result
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn count_unguarded(m: i32, n: i32, guards: Vec<Vec<i32>>, walls: Vec<Vec<i32>>) -> i32 {
Self::count_unguarded(m, n, guards, walls)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
37 changes: 37 additions & 0 deletions src/problem_2257_count_unguarded_cells_in_the_grid/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
pub mod iterative;

pub trait Solution {
fn count_unguarded(m: i32, n: i32, guards: Vec<Vec<i32>>, walls: Vec<Vec<i32>>) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
(
(
4,
6,
&[[0, 0], [1, 1], [2, 3]] as &[_],
&[[0, 1], [2, 2], [1, 4]] as &[_],
),
7,
),
((3, 3, &[[1, 1]], &[[0, 1], [1, 0], [2, 1], [1, 2]]), 4),
];

for ((m, n, guards, walls), expected) in test_cases {
assert_eq!(
S::count_unguarded(
m,
n,
guards.iter().map(Vec::from).collect(),
walls.iter().map(Vec::from).collect(),
),
expected,
);
}
}
}

0 comments on commit 6762fef

Please sign in to comment.