From 22ab95d5e2718d93f8e17ff17b9b814d87f503bc Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 21 Nov 2024 12:19:14 +0800 Subject: [PATCH] 20241121 finished. --- src/problem/mod.rs | 2 ++ src/problem/p3248_snake_in_matrix.rs | 41 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/problem/p3248_snake_in_matrix.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 847b1a5..41687e7 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -328,3 +328,5 @@ mod p661_image_smoother; mod p3243_shortest_distance_after_road_addition_queries_i; mod p3244_shortest_distance_after_road_addition_queries_ii; + +mod p3248_snake_in_matrix; diff --git a/src/problem/p3248_snake_in_matrix.rs b/src/problem/p3248_snake_in_matrix.rs new file mode 100644 index 0000000..02d7c82 --- /dev/null +++ b/src/problem/p3248_snake_in_matrix.rs @@ -0,0 +1,41 @@ +/** + * [3248] Snake in Matrix + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn final_position_of_snake(n: i32, commands: Vec) -> i32 { + let (mut x, mut y) = (0, 0); + + commands.iter().for_each(|str| match str.as_str() { + "UP" => x -= 1, + "DOWN" => x += 1, + "LEFT" => y -= 1, + "RIGHT" => y += 1, + _ => {} + }); + + x * n + y + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_3248() { + assert_eq!( + 3, + Solution::final_position_of_snake(2, vec_string!("RIGHT", "DOWN")) + ); + assert_eq!( + 1, + Solution::final_position_of_snake(3, vec_string!("DOWN", "RIGHT", "UP")) + ); + } +}