add methods to Rover

This commit is contained in:
Daniel Barlow 2023-11-02 23:44:48 +00:00
parent 023923f06b
commit 15d7b18af8

View File

@ -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,64 +20,68 @@ mod rover {
} }
} }
pub fn forward((lon, lat, direction) : &Rover) -> Rover { impl Rover {
pub fn forward(&self) -> Rover {
let Rover(lon, lat, direction) = self;
let (xoff, yoff) = velocity(&direction); let (xoff, yoff) = velocity(&direction);
(lon + xoff as i32, lat + yoff as i32, *direction) Rover(lon + xoff, lat + yoff, *direction)
} }
pub fn backward((lon, lat, direction) : &Rover) -> Rover { pub fn backward(&self) -> Rover {
let Rover(lon, lat, direction) = self;
let (xoff, yoff) = velocity(&direction); let (xoff, yoff) = velocity(&direction);
(lon - xoff as i32, lat - yoff as i32, *direction) Rover(lon - xoff, lat - yoff, *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,
Facing::W => Facing::S, // Facing::W => Facing::S,
Facing::S => Facing::E, // Facing::S => Facing::E,
Facing::E => Facing::N, // Facing::E => Facing::N,
} // }
} // }
pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover { // pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover {
match rotation { // match rotation {
Rotation::Left => { // Rotation::Left => {
let new_d = next_direction(*direction); // let new_d = next_direction(*direction);
(*lon, *lat, new_d) // (*lon, *lat, new_d)
}, // },
Rotation::Right => { // Rotation::Right => {
let new_d = next_direction(*direction); // let new_d = next_direction(*direction);
let new_d = next_direction(new_d); // let new_d = next_direction(new_d);
let new_d = next_direction(new_d); // let new_d = next_direction(new_d);
(*lon, *lat, new_d) // (*lon, *lat, new_d)
} // }
} // }
// }
} }
} }
mod driver { // mod driver {
use super::rover; // use super::rover;
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(&rover),
"b" => rover::backward(&rover), // "b" => rover::backward(&rover),
"l" => rover::rotate(&rover, rover::Rotation::Left), // "l" => rover::rotate(&rover, rover::Rotation::Left),
"r" => rover::rotate(&rover, rover::Rotation::Right), // "r" => rover::rotate(&rover, rover::Rotation::Right),
_ => rover.clone() // _ => rover.clone()
} // }
} // }
pub fn execute(rover: &rover::Rover, commands: &str) -> rover::Rover { // pub fn execute(rover: &rover::Rover, commands: &str) -> rover::Rover {
if commands.is_empty() { // if commands.is_empty() {
rover.clone() // rover.clone()
} else { // } else {
let (now, later) = commands.split_at(1); // let (now, later) = commands.split_at(1);
execute(&perform_command(now, rover), later) // execute(&perform_command(now, rover), later)
} // }
} // }
} // }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -84,63 +89,63 @@ mod tests {
#[test] #[test]
fn move_north() { fn move_north() {
let r = (1 as i32, 1 as i32, rover::Facing::N); let r = rover::Rover(1, 1, rover::Facing::N);
assert_eq!(rover::forward(&r), assert_eq!(r.forward(),
(1 as i32, 0 as i32, rover::Facing::N)); rover::Rover(1, 0, rover::Facing::N));
let r = (1 as i32, 3 as i32, rover::Facing::N); let r = rover::Rover(1, 3, rover::Facing::N);
assert_eq!(rover::forward(&r), assert_eq!(r.forward(),
(1 as i32, 2 as i32, rover::Facing::N)); rover::Rover(1, 2, rover::Facing::N));
} }
#[test] // #[test]
fn move_south() { // fn move_south() {
let r = (1 as i32, 1 as i32, rover::Facing::S); // let r = (1 as i32, 1 as i32, rover::Facing::S);
assert_eq!(rover::forward(&r), // assert_eq!(rover::forward(&r),
(1 as i32, 2 as i32, rover::Facing::S)); // (1 as i32, 2 as i32, rover::Facing::S));
} // }
#[test] // #[test]
fn move_west() { // fn move_west() {
let r = (1 as i32, 1 as i32, rover::Facing::W); // let r = (1 as i32, 1 as i32, rover::Facing::W);
assert_eq!(rover::forward(&r), // assert_eq!(rover::forward(&r),
(0 as i32, 1 as i32, rover::Facing::W)); // (0 as i32, 1 as i32, rover::Facing::W));
} // }
#[test] // #[test]
fn move_east() { // fn move_east() {
let r = (1 as i32, 1 as i32, rover::Facing::E); // let r = (1 as i32, 1 as i32, rover::Facing::E);
assert_eq!(rover::forward(&r), // assert_eq!(rover::forward(&r),
(2 as i32, 1 as i32, rover::Facing::E)); // (2 as i32, 1 as i32, rover::Facing::E));
} // }
#[test] #[test]
fn move_backward() { fn move_backward() {
let r = (1 as i32, 1 as i32, rover::Facing::E); let r = rover::Rover(1 as i32, 1 as i32, rover::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 = (1 as i32, 1 as i32, rover::Facing::E);
match rover::rotate(&r, rover::Rotation::Left) { // match rover::rotate(&r, rover::Rotation::Left) {
(_, _, facing) => assert_eq!(facing, rover::Facing::N) // (_, _, facing) => assert_eq!(facing, rover::Facing::N)
} // }
} // }
#[test] // #[test]
fn spin_right() { // fn spin_right() {
let r = (1 as i32, 1 as i32, rover::Facing::S); // let r = (1 as i32, 1 as i32, rover::Facing::S);
match rover::rotate(&r, rover::Rotation::Right) { // match rover::rotate(&r, rover::Rotation::Right) {
(_, _, facing) => assert_eq!(facing, rover::Facing::W) // (_, _, facing) => assert_eq!(facing, rover::Facing::W)
} // }
} // }
#[test] // #[test]
fn drive() { // fn drive() {
let r = (1 as i32, 1 as i32, rover::Facing::S); // let r = (1 as i32, 1 as i32, rover::Facing::S);
assert_eq!(driver::execute(&r, "ffrf"), // assert_eq!(driver::execute(&r, "ffrf"),
(0 as i32, 3 as i32, rover::Facing::W)); // (0 as i32, 3 as i32, rover::Facing::W));
} // }
} }