diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 4eca906..9bd4815 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -346,3 +346,5 @@ mod p3250_find_the_count_of_monotonic_pairs_i; mod p3232_find_if_digit_game_can_be_won; mod p51_n_queens; + +mod p3274_check_if_two_chessboard_squares_have_the_same_color; diff --git a/src/problem/p3274_check_if_two_chessboard_squares_have_the_same_color.rs b/src/problem/p3274_check_if_two_chessboard_squares_have_the_same_color.rs new file mode 100644 index 0000000..c142735 --- /dev/null +++ b/src/problem/p3274_check_if_two_chessboard_squares_have_the_same_color.rs @@ -0,0 +1,43 @@ +/** + * [3274] Check if Two Chessboard Squares Have the Same Color + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn check_two_chessboards(coordinate1: String, coordinate2: String) -> bool { + let mut c1 = coordinate1.chars(); + let mut c2 = coordinate2.chars(); + Self::get_color(c1.next().unwrap(), c1.next().unwrap()) + == Self::get_color(c2.next().unwrap(), c2.next().unwrap()) + } + + fn get_color(x: char, y: char) -> bool { + let y = y.to_digit(10).unwrap(); + match x { + 'a' | 'c' | 'e' | 'g' => y % 2 == 1, + 'b' | 'd' | 'f' | 'h' => y % 2 == 0, + _ => unreachable!(), + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3274() { + assert!(Solution::check_two_chessboards( + "a1".to_owned(), + "c3".to_owned() + )); + assert!(!Solution::check_two_chessboards( + "a1".to_owned(), + "h3".to_owned() + )); + } +}