From 53a8fda797e8c29f294b8d23e8f231dda9f8b320 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 9 Dec 2024 11:43:05 +0800 Subject: [PATCH] 20241209 finished. --- src/problem/mod.rs | 2 + ..._determine_color_of_a_chessboard_square.rs | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/problem/p1812_determine_color_of_a_chessboard_square.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index f6cca7e..9b1d1ae 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -358,3 +358,5 @@ mod p999_available_captures_for_rook; mod p688_knight_probability_in_chessboard; mod p782_transform_to_chessboard; + +mod p1812_determine_color_of_a_chessboard_square; diff --git a/src/problem/p1812_determine_color_of_a_chessboard_square.rs b/src/problem/p1812_determine_color_of_a_chessboard_square.rs new file mode 100644 index 0000000..6b5746b --- /dev/null +++ b/src/problem/p1812_determine_color_of_a_chessboard_square.rs @@ -0,0 +1,41 @@ +/** + * [1812] Determine Color of a Chessboard Square + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn square_is_white(coordinates: String) -> bool { + let mut coordinates = coordinates.chars(); + let (x, y) = (coordinates.next().unwrap(), coordinates.next().unwrap()); + + match x { + 'a' | 'c' | 'e' | 'g' => match y { + '1' | '3' | '5' | '7' => false, + '2' | '4' | '6' | '8' => true, + _ => unimplemented!(), + }, + 'b' | 'd' | 'f' | 'h' => match y { + '1' | '3' | '5' | '7' => true, + '2' | '4' | '6' | '8' => false, + _ => unimplemented!(), + }, + _ => unimplemented!(), + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1812() { + assert!(!Solution::square_is_white("a1".to_owned())); + assert!(Solution::square_is_white("h3".to_owned())); + assert!(!Solution::square_is_white("c7".to_owned())); + } +}