make Rover a struct instead of a raw tuple
This commit is contained in:
parent
023923f06b
commit
d2f123bcf6
59
src/main.rs
59
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,14 +20,16 @@ mod rover {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn forward((lon, lat, direction) : &Rover) -> Rover {
|
||||
pub fn forward(slf : &Rover) -> Rover {
|
||||
let Rover(lon, lat, direction) = slf;
|
||||
let (xoff, yoff) = velocity(&direction);
|
||||
(lon + xoff as i32, lat + yoff as i32, *direction)
|
||||
Rover(lon + xoff as i32, lat + yoff as i32, *direction)
|
||||
}
|
||||
|
||||
pub fn backward((lon, lat, direction) : &Rover) -> Rover {
|
||||
pub fn backward(slf : &Rover) -> Rover {
|
||||
let Rover(lon, lat, direction) = slf;
|
||||
let (xoff, yoff) = velocity(&direction);
|
||||
(lon - xoff as i32, lat - yoff as i32, *direction)
|
||||
Rover(lon - xoff as i32, lat - yoff as i32, *direction)
|
||||
}
|
||||
|
||||
fn next_direction(direction : Facing) -> Facing {
|
||||
@ -36,19 +39,21 @@ mod rover {
|
||||
Facing::S => Facing::E,
|
||||
Facing::E => Facing::N,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover {
|
||||
pub fn rotate(slf : &Rover, rotation: Rotation) -> Rover {
|
||||
let Rover(lon, lat, direction) = slf;
|
||||
match rotation {
|
||||
Rotation::Left => {
|
||||
let new_d = next_direction(*direction);
|
||||
(*lon, *lat, new_d)
|
||||
Rover(*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)
|
||||
Rover(*lon, *lat, new_d)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -80,67 +85,67 @@ mod driver {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use super::{rover, driver};
|
||||
use super::rover::{Rover};
|
||||
|
||||
#[test]
|
||||
fn move_north() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::N);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::N);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(1 as i32, 0 as i32, rover::Facing::N));
|
||||
Rover(1 as i32, 0 as i32, rover::Facing::N));
|
||||
|
||||
let r = (1 as i32, 3 as i32, rover::Facing::N);
|
||||
let r = Rover(1 as i32, 3 as i32, rover::Facing::N);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(1 as i32, 2 as i32, rover::Facing::N));
|
||||
Rover(1 as i32, 2 as i32, rover::Facing::N));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_south() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(1 as i32, 2 as i32, rover::Facing::S));
|
||||
Rover(1 as i32, 2 as i32, rover::Facing::S));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_west() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::W);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::W);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(0 as i32, 1 as i32, rover::Facing::W));
|
||||
Rover(0 as i32, 1 as i32, rover::Facing::W));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_east() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
|
||||
assert_eq!(rover::forward(&r),
|
||||
(2 as i32, 1 as i32, rover::Facing::E));
|
||||
Rover(2 as i32, 1 as i32, rover::Facing::E));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn move_backward() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
|
||||
let r2 = rover::forward(&r);
|
||||
assert_eq!(rover::backward(&r2), r);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spin_left() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::E);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::E);
|
||||
match rover::rotate(&r, rover::Rotation::Left) {
|
||||
(_, _, facing) => assert_eq!(facing, rover::Facing::N)
|
||||
Rover(_, _, facing) => assert_eq!(facing, rover::Facing::N)
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn spin_right() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
|
||||
match rover::rotate(&r, rover::Rotation::Right) {
|
||||
(_, _, facing) => assert_eq!(facing, rover::Facing::W)
|
||||
Rover(_, _, facing) => assert_eq!(facing, rover::Facing::W)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drive() {
|
||||
let r = (1 as i32, 1 as i32, rover::Facing::S);
|
||||
let r = Rover(1 as i32, 1 as i32, rover::Facing::S);
|
||||
assert_eq!(driver::execute(&r, "ffrf"),
|
||||
(0 as i32, 3 as i32, rover::Facing::W));
|
||||
Rover(0 as i32, 3 as i32, rover::Facing::W));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user