mirror of
https://github.com/ArthurDanjou/exercism-rust.git
synced 2026-01-14 20:19:29 +01:00
Initial commit
This commit is contained in:
34
robot-simulator/.exercism/config.json
Normal file
34
robot-simulator/.exercism/config.json
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"blurb": "Write a robot simulator.",
|
||||
"authors": [
|
||||
"IanWhitney"
|
||||
],
|
||||
"contributors": [
|
||||
"ashleygwilliams",
|
||||
"coriolinus",
|
||||
"cwhakes",
|
||||
"efx",
|
||||
"ErikSchierboom",
|
||||
"IanWhitney",
|
||||
"lutostag",
|
||||
"nfiles",
|
||||
"petertseng",
|
||||
"rofrol",
|
||||
"stringparser",
|
||||
"xakon",
|
||||
"ZapAnton"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"src/lib.rs",
|
||||
"Cargo.toml"
|
||||
],
|
||||
"test": [
|
||||
"tests/robot-simulator.rs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.rs"
|
||||
]
|
||||
},
|
||||
"source": "Inspired by an interview question at a famous company."
|
||||
}
|
||||
1
robot-simulator/.exercism/metadata.json
Normal file
1
robot-simulator/.exercism/metadata.json
Normal file
@@ -0,0 +1 @@
|
||||
{"track":"rust","exercise":"robot-simulator","id":"5906f62c2a124ee1b77c8a2ce963571e","url":"https://exercism.org/tracks/rust/exercises/robot-simulator","handle":"ArthurDanjou","is_requester":true,"auto_approve":false}
|
||||
8
robot-simulator/.gitignore
vendored
Normal file
8
robot-simulator/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
|
||||
Cargo.lock
|
||||
4
robot-simulator/Cargo.toml
Normal file
4
robot-simulator/Cargo.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
edition = "2021"
|
||||
name = "robot-simulator"
|
||||
version = "2.2.0"
|
||||
85
robot-simulator/HELP.md
Normal file
85
robot-simulator/HELP.md
Normal file
@@ -0,0 +1,85 @@
|
||||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, open the tests source file which is located in the `tests` directory
|
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||
again. Each separate test is a function with `#[test]` flag above it.
|
||||
Continue, until you pass every test.
|
||||
|
||||
If you wish to run _only ignored_ tests without editing the tests source file, use:
|
||||
|
||||
```bash
|
||||
$ cargo test -- --ignored
|
||||
```
|
||||
|
||||
If you are using Rust 1.51 or later, you can run _all_ tests with
|
||||
|
||||
```bash
|
||||
$ cargo test -- --include-ignored
|
||||
```
|
||||
|
||||
To run a specific test, for example `some_test`, you can use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test
|
||||
```
|
||||
|
||||
If the specific test is ignored, use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test -- --ignored
|
||||
```
|
||||
|
||||
To learn more about Rust tests refer to the online [test documentation][rust-tests].
|
||||
|
||||
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
|
||||
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
## Rust Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Submitting the solution
|
||||
|
||||
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.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
The GitHub [track repository][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!
|
||||
|
||||
If you want to know more about Exercism, take a look at the [contribution guide].
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
||||
[help-page]: https://exercism.org/tracks/rust/learning
|
||||
[github]: https://github.com/exercism/rust
|
||||
[contribution guide]: https://exercism.org/docs/community/contributors
|
||||
59
robot-simulator/README.md
Normal file
59
robot-simulator/README.md
Normal file
@@ -0,0 +1,59 @@
|
||||
# Robot Simulator
|
||||
|
||||
Welcome to Robot Simulator on Exercism's Rust Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Instructions
|
||||
|
||||
Write a robot simulator.
|
||||
|
||||
A robot factory's test facility needs a program to verify robot movements.
|
||||
|
||||
The robots have three possible movements:
|
||||
|
||||
- turn right
|
||||
- turn left
|
||||
- advance
|
||||
|
||||
Robots are placed on a hypothetical infinite grid, facing a particular
|
||||
direction (north, east, south, or west) at a set of {x,y} coordinates,
|
||||
e.g., {3,8}, with coordinates increasing to the north and east.
|
||||
|
||||
The robot then receives a number of instructions, at which point the
|
||||
testing facility verifies the robot's new position, and in which
|
||||
direction it is pointing.
|
||||
|
||||
- The letter-string "RAALAL" means:
|
||||
- Turn right
|
||||
- Advance twice
|
||||
- Turn left
|
||||
- Advance once
|
||||
- Turn left yet again
|
||||
- Say a robot starts at {7, 3} facing north. Then running this stream
|
||||
of instructions should leave it at {9, 4} facing west.
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @IanWhitney
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @ashleygwilliams
|
||||
- @coriolinus
|
||||
- @cwhakes
|
||||
- @efx
|
||||
- @ErikSchierboom
|
||||
- @IanWhitney
|
||||
- @lutostag
|
||||
- @nfiles
|
||||
- @petertseng
|
||||
- @rofrol
|
||||
- @stringparser
|
||||
- @xakon
|
||||
- @ZapAnton
|
||||
|
||||
### Based on
|
||||
|
||||
Inspired by an interview question at a famous company.
|
||||
77
robot-simulator/src/lib.rs
Normal file
77
robot-simulator/src/lib.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
// The code below is a stub. Just enough to satisfy the compiler.
|
||||
// In order to pass the tests you can add-to or change any of this code.
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
pub enum Direction {
|
||||
North,
|
||||
East,
|
||||
South,
|
||||
West,
|
||||
}
|
||||
|
||||
pub struct Robot {
|
||||
x: i32,
|
||||
y: i32,
|
||||
direction: Direction
|
||||
}
|
||||
|
||||
impl Robot {
|
||||
pub fn new(x: i32, y: i32, direction: Direction) -> Self {
|
||||
Self {
|
||||
x,
|
||||
y,
|
||||
direction
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn turn_right(self) -> Self {
|
||||
match self.direction {
|
||||
Direction::North => Self { direction: Direction::East, ..self },
|
||||
Direction::East => Self { direction: Direction::South, ..self },
|
||||
Direction::South => Self { direction: Direction::West, ..self },
|
||||
Direction::West => Self { direction: Direction::North, ..self }
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn turn_left(self) -> Self {
|
||||
match self.direction {
|
||||
Direction::North => Self { direction: Direction::West, ..self },
|
||||
Direction::West => Self { direction: Direction::South, ..self },
|
||||
Direction::South => Self { direction: Direction::East, ..self },
|
||||
Direction::East => Self { direction: Direction::North, ..self },
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn advance(self) -> Self {
|
||||
match self.direction {
|
||||
Direction::North => Self { y: self.y + 1, ..self },
|
||||
Direction::West => Self { x: self.x - 1, ..self },
|
||||
Direction::South => Self { y: self.y - 1, ..self },
|
||||
Direction::East => Self { x: self.x + 1, ..self },
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn instructions(self, instructions: &str) -> Self {
|
||||
instructions
|
||||
.chars()
|
||||
.fold(self, |robot, ch|
|
||||
match ch {
|
||||
'L' => robot.turn_left(),
|
||||
'R' => robot.turn_right(),
|
||||
'A' => robot.advance(),
|
||||
_ => robot
|
||||
})
|
||||
}
|
||||
|
||||
pub fn position(&self) -> (i32, i32) {
|
||||
(self.x, self.y)
|
||||
}
|
||||
|
||||
pub fn direction(&self) -> &Direction {
|
||||
&self.direction
|
||||
}
|
||||
}
|
||||
145
robot-simulator/tests/robot-simulator.rs
Normal file
145
robot-simulator/tests/robot-simulator.rs
Normal file
@@ -0,0 +1,145 @@
|
||||
use robot_simulator::*;
|
||||
|
||||
#[test]
|
||||
fn robots_are_created_with_position_and_direction() {
|
||||
let robot = Robot::new(0, 0, Direction::North);
|
||||
assert_eq!((0, 0), robot.position());
|
||||
assert_eq!(&Direction::North, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn positions_can_be_negative() {
|
||||
let robot = Robot::new(-1, -1, Direction::South);
|
||||
assert_eq!((-1, -1), robot.position());
|
||||
assert_eq!(&Direction::South, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_right_does_not_change_position() {
|
||||
let robot = Robot::new(0, 0, Direction::North).turn_right();
|
||||
assert_eq!((0, 0), robot.position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_right_from_north_points_the_robot_east() {
|
||||
let robot = Robot::new(0, 0, Direction::North).turn_right();
|
||||
assert_eq!(&Direction::East, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_right_from_east_points_the_robot_south() {
|
||||
let robot = Robot::new(0, 0, Direction::East).turn_right();
|
||||
assert_eq!(&Direction::South, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_right_from_south_points_the_robot_west() {
|
||||
let robot = Robot::new(0, 0, Direction::South).turn_right();
|
||||
assert_eq!(&Direction::West, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_right_from_west_points_the_robot_north() {
|
||||
let robot = Robot::new(0, 0, Direction::West).turn_right();
|
||||
assert_eq!(&Direction::North, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_left_does_not_change_position() {
|
||||
let robot = Robot::new(0, 0, Direction::North).turn_left();
|
||||
assert_eq!((0, 0), robot.position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_left_from_north_points_the_robot_west() {
|
||||
let robot = Robot::new(0, 0, Direction::North).turn_left();
|
||||
assert_eq!(&Direction::West, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_left_from_west_points_the_robot_south() {
|
||||
let robot = Robot::new(0, 0, Direction::West).turn_left();
|
||||
assert_eq!(&Direction::South, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_left_from_south_points_the_robot_east() {
|
||||
let robot = Robot::new(0, 0, Direction::South).turn_left();
|
||||
assert_eq!(&Direction::East, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn turning_left_from_east_points_the_robot_north() {
|
||||
let robot = Robot::new(0, 0, Direction::East).turn_left();
|
||||
assert_eq!(&Direction::North, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn advance_does_not_change_the_direction() {
|
||||
let robot = Robot::new(0, 0, Direction::North).advance();
|
||||
assert_eq!(&Direction::North, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn advance_increases_the_y_coordinate_by_one_when_facing_north() {
|
||||
let robot = Robot::new(0, 0, Direction::North).advance();
|
||||
assert_eq!((0, 1), robot.position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn advance_decreases_the_y_coordinate_by_one_when_facing_south() {
|
||||
let robot = Robot::new(0, 0, Direction::South).advance();
|
||||
assert_eq!((0, -1), robot.position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn advance_increases_the_x_coordinate_by_one_when_facing_east() {
|
||||
let robot = Robot::new(0, 0, Direction::East).advance();
|
||||
assert_eq!((1, 0), robot.position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn advance_decreases_the_x_coordinate_by_one_when_facing_west() {
|
||||
let robot = Robot::new(0, 0, Direction::West).advance();
|
||||
assert_eq!((-1, 0), robot.position());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn follow_instructions_to_move_west_and_north() {
|
||||
let robot = Robot::new(0, 0, Direction::North).instructions("LAAARALA");
|
||||
assert_eq!((-4, 1), robot.position());
|
||||
assert_eq!(&Direction::West, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn follow_instructions_to_move_west_and_south() {
|
||||
let robot = Robot::new(2, -7, Direction::East).instructions("RRAAAAALA");
|
||||
assert_eq!((-3, -8), robot.position());
|
||||
assert_eq!(&Direction::South, robot.direction());
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn follow_instructions_to_move_east_and_north() {
|
||||
let robot = Robot::new(8, 4, Direction::South).instructions("LAAARRRALLLL");
|
||||
assert_eq!((11, 5), robot.position());
|
||||
assert_eq!(&Direction::North, robot.direction());
|
||||
}
|
||||
Reference in New Issue
Block a user