diff options
Diffstat (limited to 'rust/robot-simulator')
| -rw-r--r-- | rust/robot-simulator/.exercism/metadata.json | 1 | ||||
| -rw-r--r-- | rust/robot-simulator/.gitignore | 8 | ||||
| -rw-r--r-- | rust/robot-simulator/Cargo.toml | 4 | ||||
| -rw-r--r-- | rust/robot-simulator/README.md | 108 | ||||
| -rw-r--r-- | rust/robot-simulator/src/lib.rs | 85 | ||||
| -rw-r--r-- | rust/robot-simulator/tests/robot-simulator.rs | 126 |
6 files changed, 332 insertions, 0 deletions
diff --git a/rust/robot-simulator/.exercism/metadata.json b/rust/robot-simulator/.exercism/metadata.json new file mode 100644 index 0000000..9a1e46a --- /dev/null +++ b/rust/robot-simulator/.exercism/metadata.json | |||
| @@ -0,0 +1 @@ | |||
| {"track":"rust","exercise":"robot-simulator","id":"5ec2161d3a4d47b6b7f64885d0f6e7a8","url":"https://exercism.io/my/solutions/5ec2161d3a4d47b6b7f64885d0f6e7a8","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file | |||
diff --git a/rust/robot-simulator/.gitignore b/rust/robot-simulator/.gitignore new file mode 100644 index 0000000..db7f315 --- /dev/null +++ b/rust/robot-simulator/.gitignore | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # Generated by Cargo | ||
| 2 | # will have compiled files and executables | ||
| 3 | /target/ | ||
| 4 | **/*.rs.bk | ||
| 5 | |||
| 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries | ||
| 7 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock | ||
| 8 | Cargo.lock | ||
diff --git a/rust/robot-simulator/Cargo.toml b/rust/robot-simulator/Cargo.toml new file mode 100644 index 0000000..7d5b75f --- /dev/null +++ b/rust/robot-simulator/Cargo.toml | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | [package] | ||
| 2 | edition = "2018" | ||
| 3 | name = "robot-simulator" | ||
| 4 | version = "2.2.0" | ||
diff --git a/rust/robot-simulator/README.md b/rust/robot-simulator/README.md new file mode 100644 index 0000000..9d54a6a --- /dev/null +++ b/rust/robot-simulator/README.md | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | # Robot Simulator | ||
| 2 | |||
| 3 | Write a robot simulator. | ||
| 4 | |||
| 5 | A robot factory's test facility needs a program to verify robot movements. | ||
| 6 | |||
| 7 | The robots have three possible movements: | ||
| 8 | |||
| 9 | - turn right | ||
| 10 | - turn left | ||
| 11 | - advance | ||
| 12 | |||
| 13 | Robots are placed on a hypothetical infinite grid, facing a particular | ||
| 14 | direction (north, east, south, or west) at a set of {x,y} coordinates, | ||
| 15 | e.g., {3,8}, with coordinates increasing to the north and east. | ||
| 16 | |||
| 17 | The robot then receives a number of instructions, at which point the | ||
| 18 | testing facility verifies the robot's new position, and in which | ||
| 19 | direction it is pointing. | ||
| 20 | |||
| 21 | - The letter-string "RAALAL" means: | ||
| 22 | - Turn right | ||
| 23 | - Advance twice | ||
| 24 | - Turn left | ||
| 25 | - Advance once | ||
| 26 | - Turn left yet again | ||
| 27 | - Say a robot starts at {7, 3} facing north. Then running this stream | ||
| 28 | of instructions should leave it at {9, 4} facing west. | ||
| 29 | |||
| 30 | ## Rust Installation | ||
| 31 | |||
| 32 | Refer to the [exercism help page][help-page] for Rust installation and learning | ||
| 33 | resources. | ||
| 34 | |||
| 35 | ## Writing the Code | ||
| 36 | |||
| 37 | Execute the tests with: | ||
| 38 | |||
| 39 | ```bash | ||
| 40 | $ cargo test | ||
| 41 | ``` | ||
| 42 | |||
| 43 | All but the first test have been ignored. After you get the first test to | ||
| 44 | pass, open the tests source file which is located in the `tests` directory | ||
| 45 | and remove the `#[ignore]` flag from the next test and get the tests to pass | ||
| 46 | again. Each separate test is a function with `#[test]` flag above it. | ||
| 47 | Continue, until you pass every test. | ||
| 48 | |||
| 49 | If you wish to run all ignored tests without editing the tests source file, use: | ||
| 50 | |||
| 51 | ```bash | ||
| 52 | $ cargo test -- --ignored | ||
| 53 | ``` | ||
| 54 | |||
| 55 | To run a specific test, for example `some_test`, you can use: | ||
| 56 | |||
| 57 | ```bash | ||
| 58 | $ cargo test some_test | ||
| 59 | ``` | ||
| 60 | |||
| 61 | If the specific test is ignored use: | ||
| 62 | |||
| 63 | ```bash | ||
| 64 | $ cargo test some_test -- --ignored | ||
| 65 | ``` | ||
| 66 | |||
| 67 | To learn more about Rust tests refer to the [online test documentation][rust-tests] | ||
| 68 | |||
| 69 | Make sure to read the [Modules][modules] chapter if you | ||
| 70 | haven't already, it will help you with organizing your files. | ||
| 71 | |||
| 72 | ## Further improvements | ||
| 73 | |||
| 74 | After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution. | ||
| 75 | |||
| 76 | To format your solution, inside the solution directory use | ||
| 77 | |||
| 78 | ```bash | ||
| 79 | cargo fmt | ||
| 80 | ``` | ||
| 81 | |||
| 82 | To see, if your solution contains some common ineffective use cases, inside the solution directory use | ||
| 83 | |||
| 84 | ```bash | ||
| 85 | cargo clippy --all-targets | ||
| 86 | ``` | ||
| 87 | |||
| 88 | ## Submitting the solution | ||
| 89 | |||
| 90 | Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. | ||
| 91 | |||
| 92 | ## Feedback, Issues, Pull Requests | ||
| 93 | |||
| 94 | The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! | ||
| 95 | |||
| 96 | If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md). | ||
| 97 | |||
| 98 | [help-page]: https://exercism.io/tracks/rust/learning | ||
| 99 | [modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html | ||
| 100 | [cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html | ||
| 101 | [rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html | ||
| 102 | |||
| 103 | ## Source | ||
| 104 | |||
| 105 | Inspired by an interview question at a famous company. | ||
| 106 | |||
| 107 | ## Submitting Incomplete Solutions | ||
| 108 | It's possible to submit an incomplete solution so you can see how others have completed the exercise. | ||
diff --git a/rust/robot-simulator/src/lib.rs b/rust/robot-simulator/src/lib.rs new file mode 100644 index 0000000..d6b6719 --- /dev/null +++ b/rust/robot-simulator/src/lib.rs | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | // The code below is a stub. Just enough to satisfy the compiler. | ||
| 2 | // In order to pass the tests you can add-to or change any of this code. | ||
| 3 | |||
| 4 | #[derive(PartialEq, Debug)] | ||
| 5 | pub enum Direction { | ||
| 6 | North, | ||
| 7 | East, | ||
| 8 | South, | ||
| 9 | West, | ||
| 10 | } | ||
| 11 | |||
| 12 | pub struct Robot { | ||
| 13 | x: i32, | ||
| 14 | y: i32, | ||
| 15 | d: Direction, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl Robot { | ||
| 19 | pub fn new(x: i32, y: i32, d: Direction) -> Self { | ||
| 20 | Robot { x, y, d } | ||
| 21 | } | ||
| 22 | |||
| 23 | pub fn turn_right(self) -> Self { | ||
| 24 | Robot { | ||
| 25 | d: { | ||
| 26 | use Direction::*; | ||
| 27 | match self.d { | ||
| 28 | North => East, | ||
| 29 | East => South, | ||
| 30 | South => West, | ||
| 31 | West => North, | ||
| 32 | } | ||
| 33 | }, | ||
| 34 | ..self | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | pub fn turn_left(self) -> Self { | ||
| 39 | Robot { | ||
| 40 | d: { | ||
| 41 | use Direction::*; | ||
| 42 | match self.d { | ||
| 43 | North => West, | ||
| 44 | East => North, | ||
| 45 | South => East, | ||
| 46 | West => South, | ||
| 47 | } | ||
| 48 | }, | ||
| 49 | ..self | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | pub fn advance(self) -> Self { | ||
| 54 | Robot { | ||
| 55 | x: match self.d { | ||
| 56 | Direction::West => self.x - 1, | ||
| 57 | Direction::East => self.x + 1, | ||
| 58 | _ => self.x, | ||
| 59 | }, | ||
| 60 | y: match self.d { | ||
| 61 | Direction::North => self.y + 1, | ||
| 62 | Direction::South => self.y - 1, | ||
| 63 | _ => self.y, | ||
| 64 | }, | ||
| 65 | ..self | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | pub fn instructions(self, instructions: &str) -> Self { | ||
| 70 | instructions.chars().fold(self, |acc, action| match action { | ||
| 71 | 'A' => acc.advance(), | ||
| 72 | 'R' => acc.turn_right(), | ||
| 73 | 'L' => acc.turn_left(), | ||
| 74 | _ => acc, | ||
| 75 | }) | ||
| 76 | } | ||
| 77 | |||
| 78 | pub fn position(&self) -> (i32, i32) { | ||
| 79 | (self.x, self.y) | ||
| 80 | } | ||
| 81 | |||
| 82 | pub fn direction(&self) -> &Direction { | ||
| 83 | &self.d | ||
| 84 | } | ||
| 85 | } | ||
diff --git a/rust/robot-simulator/tests/robot-simulator.rs b/rust/robot-simulator/tests/robot-simulator.rs new file mode 100644 index 0000000..14235e9 --- /dev/null +++ b/rust/robot-simulator/tests/robot-simulator.rs | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | use robot_simulator::*; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn robots_are_created_with_position_and_direction() { | ||
| 5 | let robot = Robot::new(0, 0, Direction::North); | ||
| 6 | assert_eq!((0, 0), robot.position()); | ||
| 7 | assert_eq!(&Direction::North, robot.direction()); | ||
| 8 | } | ||
| 9 | |||
| 10 | #[test] | ||
| 11 | fn positions_can_be_negative() { | ||
| 12 | let robot = Robot::new(-1, -1, Direction::South); | ||
| 13 | assert_eq!((-1, -1), robot.position()); | ||
| 14 | assert_eq!(&Direction::South, robot.direction()); | ||
| 15 | } | ||
| 16 | |||
| 17 | #[test] | ||
| 18 | fn turning_right_does_not_change_position() { | ||
| 19 | let robot = Robot::new(0, 0, Direction::North).turn_right(); | ||
| 20 | assert_eq!((0, 0), robot.position()); | ||
| 21 | } | ||
| 22 | |||
| 23 | #[test] | ||
| 24 | fn turning_right_from_north_points_the_robot_east() { | ||
| 25 | let robot = Robot::new(0, 0, Direction::North).turn_right(); | ||
| 26 | assert_eq!(&Direction::East, robot.direction()); | ||
| 27 | } | ||
| 28 | |||
| 29 | #[test] | ||
| 30 | fn turning_right_from_east_points_the_robot_south() { | ||
| 31 | let robot = Robot::new(0, 0, Direction::East).turn_right(); | ||
| 32 | assert_eq!(&Direction::South, robot.direction()); | ||
| 33 | } | ||
| 34 | |||
| 35 | #[test] | ||
| 36 | fn turning_right_from_south_points_the_robot_west() { | ||
| 37 | let robot = Robot::new(0, 0, Direction::South).turn_right(); | ||
| 38 | assert_eq!(&Direction::West, robot.direction()); | ||
| 39 | } | ||
| 40 | |||
| 41 | #[test] | ||
| 42 | fn turning_right_from_west_points_the_robot_north() { | ||
| 43 | let robot = Robot::new(0, 0, Direction::West).turn_right(); | ||
| 44 | assert_eq!(&Direction::North, robot.direction()); | ||
| 45 | } | ||
| 46 | |||
| 47 | #[test] | ||
| 48 | fn turning_left_does_not_change_position() { | ||
| 49 | let robot = Robot::new(0, 0, Direction::North).turn_left(); | ||
| 50 | assert_eq!((0, 0), robot.position()); | ||
| 51 | } | ||
| 52 | |||
| 53 | #[test] | ||
| 54 | fn turning_left_from_north_points_the_robot_west() { | ||
| 55 | let robot = Robot::new(0, 0, Direction::North).turn_left(); | ||
| 56 | assert_eq!(&Direction::West, robot.direction()); | ||
| 57 | } | ||
| 58 | |||
| 59 | #[test] | ||
| 60 | fn turning_left_from_west_points_the_robot_south() { | ||
| 61 | let robot = Robot::new(0, 0, Direction::West).turn_left(); | ||
| 62 | assert_eq!(&Direction::South, robot.direction()); | ||
| 63 | } | ||
| 64 | |||
| 65 | #[test] | ||
| 66 | fn turning_left_from_south_points_the_robot_east() { | ||
| 67 | let robot = Robot::new(0, 0, Direction::South).turn_left(); | ||
| 68 | assert_eq!(&Direction::East, robot.direction()); | ||
| 69 | } | ||
| 70 | |||
| 71 | #[test] | ||
| 72 | fn turning_left_from_east_points_the_robot_north() { | ||
| 73 | let robot = Robot::new(0, 0, Direction::East).turn_left(); | ||
| 74 | assert_eq!(&Direction::North, robot.direction()); | ||
| 75 | } | ||
| 76 | |||
| 77 | #[test] | ||
| 78 | fn advance_does_not_change_the_direction() { | ||
| 79 | let robot = Robot::new(0, 0, Direction::North).advance(); | ||
| 80 | assert_eq!(&Direction::North, robot.direction()); | ||
| 81 | } | ||
| 82 | |||
| 83 | #[test] | ||
| 84 | fn advance_increases_the_y_coordinate_by_one_when_facing_north() { | ||
| 85 | let robot = Robot::new(0, 0, Direction::North).advance(); | ||
| 86 | assert_eq!((0, 1), robot.position()); | ||
| 87 | } | ||
| 88 | |||
| 89 | #[test] | ||
| 90 | fn advance_decreases_the_y_coordinate_by_one_when_facing_south() { | ||
| 91 | let robot = Robot::new(0, 0, Direction::South).advance(); | ||
| 92 | assert_eq!((0, -1), robot.position()); | ||
| 93 | } | ||
| 94 | |||
| 95 | #[test] | ||
| 96 | fn advance_increases_the_x_coordinate_by_one_when_facing_east() { | ||
| 97 | let robot = Robot::new(0, 0, Direction::East).advance(); | ||
| 98 | assert_eq!((1, 0), robot.position()); | ||
| 99 | } | ||
| 100 | |||
| 101 | #[test] | ||
| 102 | fn advance_decreases_the_x_coordinate_by_one_when_facing_west() { | ||
| 103 | let robot = Robot::new(0, 0, Direction::West).advance(); | ||
| 104 | assert_eq!((-1, 0), robot.position()); | ||
| 105 | } | ||
| 106 | |||
| 107 | #[test] | ||
| 108 | fn follow_instructions_to_move_west_and_north() { | ||
| 109 | let robot = Robot::new(0, 0, Direction::North).instructions("LAAARALA"); | ||
| 110 | assert_eq!((-4, 1), robot.position()); | ||
| 111 | assert_eq!(&Direction::West, robot.direction()); | ||
| 112 | } | ||
| 113 | |||
| 114 | #[test] | ||
| 115 | fn follow_instructions_to_move_west_and_south() { | ||
| 116 | let robot = Robot::new(2, -7, Direction::East).instructions("RRAAAAALA"); | ||
| 117 | assert_eq!((-3, -8), robot.position()); | ||
| 118 | assert_eq!(&Direction::South, robot.direction()); | ||
| 119 | } | ||
| 120 | |||
| 121 | #[test] | ||
| 122 | fn follow_instructions_to_move_east_and_north() { | ||
| 123 | let robot = Robot::new(8, 4, Direction::South).instructions("LAAARRRALLLL"); | ||
| 124 | assert_eq!((11, 5), robot.position()); | ||
| 125 | assert_eq!(&Direction::North, robot.direction()); | ||
| 126 | } | ||
