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)]
|
||||
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) {
|
||||
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 {
|
||||
match direction {
|
||||
Facing::N => Facing::W,
|
||||
@ -38,18 +29,34 @@ mod rover {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover {
|
||||
match rotation {
|
||||
Rotation::Left => {
|
||||
let new_d = next_direction(*direction);
|
||||
(*lon, *lat, new_d)
|
||||
},
|
||||
Rotation::Right => {
|
||||
let new_d = next_direction(*direction);
|
||||
let new_d = next_direction(new_d);
|
||||
let new_d = next_direction(new_d);
|
||||
(*lon, *lat, new_d)
|
||||
}
|
||||
impl Rover {
|
||||
pub fn forward(&self) -> Rover {
|
||||
let Rover(lon, lat, direction) = self;
|
||||
let (xoff, yoff) = velocity(&direction);
|
||||
Rover(lon + xoff, lat + yoff, *direction)
|
||||
}
|
||||
|
||||
pub fn backward(&self) -> Rover {
|
||||
let Rover(lon, lat, direction) = self;
|
||||
let (xoff, yoff) = velocity(&direction);
|
||||
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 {
|
||||
match command {
|
||||
"f" => rover::forward(&rover),
|
||||
"b" => rover::backward(&rover),
|
||||
"l" => rover::rotate(&rover, rover::Rotation::Left),
|
||||
"r" => rover::rotate(&rover, rover::Rotation::Right),
|
||||
"f" => rover.forward(),
|
||||
"b" => rover.backward(),
|
||||
"l" => rover.rotate(rover::Rotation::Left),
|
||||
"r" => rover.rotate(rover::Rotation::Right),
|
||||
_ => rover.clone()
|
||||
}
|
||||
}
|
||||
@ -80,67 +87,67 @@ mod driver {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::driver;
|
||||
use super::rover::{Rover, Facing,Rotation};
|
||||
|
||||
#[test]
|
||||
fn move_north() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::N);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(1 as i32, 0 as i32, rover::Facing::N));
|
||||
let r = Rover(1, 1, Facing::N);
|
||||
assert_eq!(r.forward(),
|
||||
Rover(1, 0, Facing::N));
|
||||
|
||||
let r = (1 as i32, 3 as i32, rover::Facing::N);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(1 as i32, 2 as i32, rover::Facing::N));
|
||||
let r = Rover(1, 3, Facing::N);
|
||||
assert_eq!(r.forward(),
|
||||
Rover(1, 2, Facing::N));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_south() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(1 as i32, 2 as i32, rover::Facing::S));
|
||||
let r = Rover(1, 1, Facing::S);
|
||||
assert_eq!(r.forward(),
|
||||
Rover(1, 2, Facing::S));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_west() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::W);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(0 as i32, 1 as i32, rover::Facing::W));
|
||||
let r = Rover(1, 1, Facing::W);
|
||||
assert_eq!(r.forward(),
|
||||
Rover(0, 1, Facing::W));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_east() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(2 as i32, 1 as i32, rover::Facing::E));
|
||||
let r = Rover(1, 1, Facing::E);
|
||||
assert_eq!(r.forward(),
|
||||
Rover(2, 1, Facing::E));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_backward() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
||||
let r2 = rover::forward(&r);
|
||||
assert_eq!(rover::backward(&r2), r);
|
||||
let r = Rover(1, 1, Facing::E);
|
||||
let r2 = r.forward();
|
||||
assert_eq!(r2.backward(), r);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spin_left() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
||||
match rover::rotate(&r, rover::Rotation::Left) {
|
||||
(_, _, facing) => assert_eq!(facing, rover::Facing::N)
|
||||
let r = Rover(1, 1, Facing::E);
|
||||
match r.rotate(Rotation::Left) {
|
||||
Rover(_, _, facing) => assert_eq!(facing, Facing::N)
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn spin_right() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
||||
match rover::rotate(&r, rover::Rotation::Right) {
|
||||
(_, _, facing) => assert_eq!(facing, rover::Facing::W)
|
||||
let r = Rover(1, 1, Facing::S);
|
||||
match r.rotate(Rotation::Right) {
|
||||
Rover(_, _, facing) => assert_eq!(facing, Facing::W)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
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"),
|
||||
(0 as i32, 3 as i32, rover::Facing::W));
|
||||
Rover(0, 3, Facing::W));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user