Skip to content

Commit 1fc99b4

Browse files
committed
Add problem 874: Walking Robot Simulation
1 parent a40b2d6 commit 1fc99b4

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ pub mod problem_0868_binary_gap;
716716
pub mod problem_0869_reordered_power_of_2;
717717
pub mod problem_0870_advantage_shuffle;
718718
pub mod problem_0872_leaf_similar_trees;
719+
pub mod problem_0874_walking_robot_simulation;
719720
pub mod problem_1143_longest_common_subsequence;
720721
pub mod problem_1192_critical_connections_in_a_network;
721722
pub mod problem_1342_number_of_steps_to_reduce_a_number_to_zero;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::collections::HashSet;
6+
use std::convert::TryInto;
7+
8+
impl Solution {
9+
pub fn robot_sim(commands: Vec<i32>, obstacles: Vec<Vec<i32>>) -> i32 {
10+
let mut position = (0, 0);
11+
let mut direction = (0, 1);
12+
13+
let obstacles = obstacles
14+
.into_iter()
15+
.map(|position| {
16+
let [x, y]: [i32; 2] = position.as_slice().try_into().unwrap();
17+
18+
(x, y)
19+
})
20+
.collect::<HashSet<_>>();
21+
22+
let mut result = 0;
23+
24+
for command in commands {
25+
match command {
26+
-2 => direction = (-direction.1, direction.0),
27+
-1 => direction = (direction.1, -direction.0),
28+
mut distance => loop {
29+
let next_position = (position.0 + direction.0, position.1 + direction.1);
30+
31+
if obstacles.contains(&next_position) {
32+
break;
33+
}
34+
35+
position = next_position;
36+
distance -= 1;
37+
38+
result = result.max(position.0.pow(2) + position.1.pow(2));
39+
40+
if distance == 0 {
41+
break;
42+
}
43+
},
44+
}
45+
}
46+
47+
result
48+
}
49+
}
50+
51+
// ------------------------------------------------------ snip ------------------------------------------------------ //
52+
53+
impl super::Solution for Solution {
54+
fn robot_sim(commands: Vec<i32>, obstacles: Vec<Vec<i32>>) -> i32 {
55+
Self::robot_sim(commands, obstacles)
56+
}
57+
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
#[test]
62+
fn test_solution() {
63+
super::super::tests::run::<super::Solution>();
64+
}
65+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub mod iterative;
2+
3+
pub trait Solution {
4+
fn robot_sim(commands: Vec<i32>, obstacles: Vec<Vec<i32>>) -> i32;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((&[4, -1, 3] as &[_], &[] as &[[_; 2]]), 25),
14+
((&[4, -1, 4, -2, 4], &[[2, 4]]), 65),
15+
((&[6, -1, -1, 6], &[]), 36),
16+
];
17+
18+
for ((commands, obstacles), expected) in test_cases {
19+
assert_eq!(
20+
S::robot_sim(commands.to_vec(), obstacles.iter().copied().map(Vec::from).collect()),
21+
expected
22+
);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)