Skip to content

Commit

Permalink
Add problem 1886: Determine Whether Matrix Can Be Obtained By Rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Oct 18, 2023
1 parent b84d25b commit d542e0a
Show file tree
Hide file tree
Showing 3 changed files with 87 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 @@ -1450,6 +1450,7 @@ pub mod problem_1877_minimize_maximum_pair_sum_in_array;
pub mod problem_1880_check_if_word_equals_summation_of_two_words;
pub mod problem_1881_maximum_value_after_insertion;
pub mod problem_1884_egg_drop_with_2_eggs_and_n_floors;
pub mod problem_1886_determine_whether_matrix_can_be_obtained_by_rotation;

#[cfg(test)]
mod test_utilities;
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pub struct Solution;

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

impl Solution {
fn check_0_degree(lhs: &[Vec<i32>], rhs: &[Vec<i32>]) -> bool {
lhs == rhs
}

fn check_90_degrees(lhs: &[Vec<i32>], rhs: &[Vec<i32>]) -> bool {
lhs.iter()
.rev()
.enumerate()
.all(|(i, row)| row.iter().zip(rhs).all(|(&x, rhs_row)| x == rhs_row[i]))
}

fn check_180_degrees(lhs: &[Vec<i32>], rhs: &[Vec<i32>]) -> bool {
lhs.iter()
.zip(rhs.iter().rev())
.all(|(lhs_row, rhs_row)| lhs_row.iter().zip(rhs_row.iter().rev()).all(|(x, y)| x == y))
}

fn check_270_degrees(lhs: &[Vec<i32>], rhs: &[Vec<i32>]) -> bool {
lhs.iter().enumerate().all(|(i, lhs_row)| {
lhs_row
.iter()
.zip(rhs.iter().rev())
.all(|(&x, rhs_row)| x == rhs_row[i])
})
}

pub fn find_rotation(mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool {
let lhs = mat.as_slice();
let rhs = target.as_slice();

Self::check_0_degree(lhs, rhs)
|| Self::check_90_degrees(lhs, rhs)
|| Self::check_180_degrees(lhs, rhs)
|| Self::check_270_degrees(lhs, rhs)
}
}

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

impl super::Solution for Solution {
fn find_rotation(mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool {
Self::find_rotation(mat, target)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pub mod brute_force;

pub trait Solution {
fn find_rotation(mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool;
}

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

pub fn run<S: Solution>() {
let test_cases = [
(
(&[[0, 1], [1, 0]] as &dyn Matrix<_>, &[[1, 0], [0, 1]] as &dyn Matrix<_>),
true,
),
((&[[0, 1], [1, 1]], &[[1, 0], [0, 1]]), false),
(
(&[[0, 0, 0], [0, 1, 0], [1, 1, 1]], &[[1, 1, 1], [0, 1, 0], [0, 0, 0]]),
true,
),
];

for ((mat, target), expected) in test_cases {
assert_eq!(S::find_rotation(mat.to_vec(), target.to_vec()), expected);
}
}
}

0 comments on commit d542e0a

Please sign in to comment.