From d542e0aa94ff3a0af17b5847dda5a9c755125a65 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Wed, 18 Oct 2023 23:22:51 +0800 Subject: [PATCH] Add problem 1886: Determine Whether Matrix Can Be Obtained By Rotation --- src/lib.rs | 1 + .../brute_force.rs | 57 +++++++++++++++++++ .../mod.rs | 29 ++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/brute_force.rs create mode 100644 src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/mod.rs diff --git a/src/lib.rs b/src/lib.rs index 1e5fb871..72543076 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/brute_force.rs b/src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/brute_force.rs new file mode 100644 index 00000000..05490acc --- /dev/null +++ b/src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/brute_force.rs @@ -0,0 +1,57 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + fn check_0_degree(lhs: &[Vec], rhs: &[Vec]) -> bool { + lhs == rhs + } + + fn check_90_degrees(lhs: &[Vec], rhs: &[Vec]) -> 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], rhs: &[Vec]) -> 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], rhs: &[Vec]) -> 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>, target: Vec>) -> 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>, target: Vec>) -> bool { + Self::find_rotation(mat, target) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/mod.rs b/src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/mod.rs new file mode 100644 index 00000000..e0dcb145 --- /dev/null +++ b/src/problem_1886_determine_whether_matrix_can_be_obtained_by_rotation/mod.rs @@ -0,0 +1,29 @@ +pub mod brute_force; + +pub trait Solution { + fn find_rotation(mat: Vec>, target: Vec>) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + use crate::test_utilities::Matrix; + + pub fn run() { + 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); + } + } +}