Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
95d201eb2f | |||
9a9bad4f71 | |||
3af88ea712 | |||
4f3fb62432 | |||
1cc78f1e54 | |||
d2f123bcf6 |
117
src/main.rs
117
src/main.rs
@ -8,7 +8,8 @@ mod rover {
|
|||||||
#[derive(Debug, PartialEq, Copy, Clone)]
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
pub enum Rotation { Left, Right }
|
pub enum Rotation { Left, Right }
|
||||||
|
|
||||||
pub type Rover = (i32, i32, Facing);
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
|
pub struct Rover(pub i32, pub i32, pub Facing);
|
||||||
|
|
||||||
fn velocity(direction : &Facing) -> (i32, i32) {
|
fn velocity(direction : &Facing) -> (i32, i32) {
|
||||||
match direction {
|
match direction {
|
||||||
@ -19,16 +20,6 @@ mod rover {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn forward((lon, lat, direction) : &Rover) -> Rover {
|
|
||||||
let (xoff, yoff) = velocity(&direction);
|
|
||||||
(lon + xoff as i32, lat + yoff as i32, *direction)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn backward((lon, lat, direction) : &Rover) -> Rover {
|
|
||||||
let (xoff, yoff) = velocity(&direction);
|
|
||||||
(lon - xoff as i32, lat - yoff as i32, *direction)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn next_direction(direction : Facing) -> Facing {
|
fn next_direction(direction : Facing) -> Facing {
|
||||||
match direction {
|
match direction {
|
||||||
Facing::N => Facing::W,
|
Facing::N => Facing::W,
|
||||||
@ -38,18 +29,34 @@ mod rover {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover {
|
impl Rover {
|
||||||
match rotation {
|
pub fn forward(&self) -> Rover {
|
||||||
Rotation::Left => {
|
let Rover(lon, lat, direction) = self;
|
||||||
let new_d = next_direction(*direction);
|
let (xoff, yoff) = velocity(&direction);
|
||||||
(*lon, *lat, new_d)
|
Rover(lon + xoff, lat + yoff, *direction)
|
||||||
},
|
}
|
||||||
Rotation::Right => {
|
|
||||||
let new_d = next_direction(*direction);
|
pub fn backward(&self) -> Rover {
|
||||||
let new_d = next_direction(new_d);
|
let Rover(lon, lat, direction) = self;
|
||||||
let new_d = next_direction(new_d);
|
let (xoff, yoff) = velocity(&direction);
|
||||||
(*lon, *lat, new_d)
|
Rover(lon - xoff, lat - yoff, *direction)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn rotate(&self, rotation: Rotation) -> Rover {
|
||||||
|
let Rover(lon, lat, direction) = self;
|
||||||
|
let new_direction = match rotation {
|
||||||
|
Rotation::Left => {
|
||||||
|
next_direction(*direction)
|
||||||
|
},
|
||||||
|
Rotation::Right => {
|
||||||
|
next_direction(
|
||||||
|
next_direction(
|
||||||
|
next_direction(*direction)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Rover(*lon, *lat, new_direction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -60,10 +67,10 @@ mod driver {
|
|||||||
|
|
||||||
fn perform_command(command: &str, rover: &rover::Rover) -> rover::Rover {
|
fn perform_command(command: &str, rover: &rover::Rover) -> rover::Rover {
|
||||||
match command {
|
match command {
|
||||||
"f" => rover::forward(&rover),
|
"f" => rover.forward(),
|
||||||
"b" => rover::backward(&rover),
|
"b" => rover.backward(),
|
||||||
"l" => rover::rotate(&rover, rover::Rotation::Left),
|
"l" => rover.rotate(rover::Rotation::Left),
|
||||||
"r" => rover::rotate(&rover, rover::Rotation::Right),
|
"r" => rover.rotate(rover::Rotation::Right),
|
||||||
_ => rover.clone()
|
_ => rover.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -80,67 +87,67 @@ mod driver {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::driver;
|
||||||
|
use super::rover::{Rover, Facing,Rotation};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_north() {
|
fn move_north() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::N);
|
let r = Rover(1, 1, Facing::N);
|
||||||
assert_eq!(rover::forward(&r),
|
assert_eq!(r.forward(),
|
||||||
(1 as i32, 0 as i32, rover::Facing::N));
|
Rover(1, 0, Facing::N));
|
||||||
|
|
||||||
let r = (1 as i32, 3 as i32, rover::Facing::N);
|
let r = Rover(1, 3, Facing::N);
|
||||||
assert_eq!(rover::forward(&r),
|
assert_eq!(r.forward(),
|
||||||
(1 as i32, 2 as i32, rover::Facing::N));
|
Rover(1, 2, Facing::N));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_south() {
|
fn move_south() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
let r = Rover(1, 1, Facing::S);
|
||||||
assert_eq!(rover::forward(&r),
|
assert_eq!(r.forward(),
|
||||||
(1 as i32, 2 as i32, rover::Facing::S));
|
Rover(1, 2, Facing::S));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_west() {
|
fn move_west() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::W);
|
let r = Rover(1, 1, Facing::W);
|
||||||
assert_eq!(rover::forward(&r),
|
assert_eq!(r.forward(),
|
||||||
(0 as i32, 1 as i32, rover::Facing::W));
|
Rover(0, 1, Facing::W));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_east() {
|
fn move_east() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
let r = Rover(1, 1, Facing::E);
|
||||||
assert_eq!(rover::forward(&r),
|
assert_eq!(r.forward(),
|
||||||
(2 as i32, 1 as i32, rover::Facing::E));
|
Rover(2, 1, Facing::E));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_backward() {
|
fn move_backward() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
let r = Rover(1, 1, Facing::E);
|
||||||
let r2 = rover::forward(&r);
|
let r2 = r.forward();
|
||||||
assert_eq!(rover::backward(&r2), r);
|
assert_eq!(r2.backward(), r);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn spin_left() {
|
fn spin_left() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
let r = Rover(1, 1, Facing::E);
|
||||||
match rover::rotate(&r, rover::Rotation::Left) {
|
match r.rotate(Rotation::Left) {
|
||||||
(_, _, facing) => assert_eq!(facing, rover::Facing::N)
|
Rover(_, _, facing) => assert_eq!(facing, Facing::N)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn spin_right() {
|
fn spin_right() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
let r = Rover(1, 1, Facing::S);
|
||||||
match rover::rotate(&r, rover::Rotation::Right) {
|
match r.rotate(Rotation::Right) {
|
||||||
(_, _, facing) => assert_eq!(facing, rover::Facing::W)
|
Rover(_, _, facing) => assert_eq!(facing, Facing::W)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn drive() {
|
fn drive() {
|
||||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
let r = Rover(1, 1, Facing::S);
|
||||||
assert_eq!(driver::execute(&r, "ffrf"),
|
assert_eq!(driver::execute(&r, "ffrf"),
|
||||||
(0 as i32, 3 as i32, rover::Facing::W));
|
Rover(0, 3, Facing::W));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user