Compare commits

..

1 Commits
main ... wip

Author SHA1 Message Date
15d7b18af8 add methods to Rover 2023-11-02 23:44:48 +00:00

View File

@ -20,15 +20,6 @@ mod rover {
} }
} }
fn next_direction(direction : Facing) -> Facing {
match direction {
Facing::N => Facing::W,
Facing::W => Facing::S,
Facing::S => Facing::E,
Facing::E => Facing::N,
}
}
impl Rover { impl Rover {
pub fn forward(&self) -> Rover { pub fn forward(&self) -> Rover {
let Rover(lon, lat, direction) = self; let Rover(lon, lat, direction) = self;
@ -42,112 +33,119 @@ mod rover {
Rover(lon - xoff, lat - yoff, *direction) Rover(lon - xoff, lat - yoff, *direction)
} }
pub fn rotate(&self, rotation: Rotation) -> Rover { // fn next_direction(direction : Facing) -> Facing {
let Rover(lon, lat, direction) = self; // match direction {
let new_direction = match rotation { // Facing::N => Facing::W,
Rotation::Left => { // Facing::W => Facing::S,
next_direction(*direction) // Facing::S => Facing::E,
}, // Facing::E => Facing::N,
Rotation::Right => { // }
next_direction( // }
next_direction(
next_direction(*direction) // pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover {
) // match rotation {
) // Rotation::Left => {
} // let new_d = next_direction(*direction);
}; // (*lon, *lat, new_d)
Rover(*lon, *lat, new_direction) // },
} // 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)
// }
// }
// }
} }
} }
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(), // "f" => rover::forward(&rover),
"b" => rover.backward(), // "b" => rover::backward(&rover),
"l" => rover.rotate(rover::Rotation::Left), // "l" => rover::rotate(&rover, rover::Rotation::Left),
"r" => rover.rotate(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 {
use super::driver; use super::*;
use super::rover::{Rover, Facing,Rotation};
#[test] #[test]
fn move_north() { fn move_north() {
let r = Rover(1, 1, Facing::N); let r = rover::Rover(1, 1, rover::Facing::N);
assert_eq!(r.forward(), assert_eq!(r.forward(),
Rover(1, 0, Facing::N)); rover::Rover(1, 0, rover::Facing::N));
let r = Rover(1, 3, Facing::N); let r = rover::Rover(1, 3, rover::Facing::N);
assert_eq!(r.forward(), assert_eq!(r.forward(),
Rover(1, 2, Facing::N)); rover::Rover(1, 2, rover::Facing::N));
} }
#[test] // #[test]
fn move_south() { // fn move_south() {
let r = Rover(1, 1, Facing::S); // let r = (1 as i32, 1 as i32, rover::Facing::S);
assert_eq!(r.forward(), // assert_eq!(rover::forward(&r),
Rover(1, 2, Facing::S)); // (1 as i32, 2 as i32, rover::Facing::S));
} // }
#[test] // #[test]
fn move_west() { // fn move_west() {
let r = Rover(1, 1, Facing::W); // let r = (1 as i32, 1 as i32, rover::Facing::W);
assert_eq!(r.forward(), // assert_eq!(rover::forward(&r),
Rover(0, 1, Facing::W)); // (0 as i32, 1 as i32, rover::Facing::W));
} // }
#[test] // #[test]
fn move_east() { // fn move_east() {
let r = Rover(1, 1, Facing::E); // let r = (1 as i32, 1 as i32, rover::Facing::E);
assert_eq!(r.forward(), // assert_eq!(rover::forward(&r),
Rover(2, 1, Facing::E)); // (2 as i32, 1 as i32, rover::Facing::E));
} // }
#[test] #[test]
fn move_backward() { fn move_backward() {
let r = Rover(1, 1, Facing::E); let r = rover::Rover(1 as i32, 1 as i32, rover::Facing::E);
let r2 = r.forward(); let r2 = r.forward();
assert_eq!(r2.backward(), r); assert_eq!(r2.backward(), r);
} }
#[test] // #[test]
fn spin_left() { // fn spin_left() {
let r = Rover(1, 1, Facing::E); // let r = (1 as i32, 1 as i32, rover::Facing::E);
match r.rotate(Rotation::Left) { // match rover::rotate(&r, rover::Rotation::Left) {
Rover(_, _, facing) => assert_eq!(facing, Facing::N) // (_, _, facing) => assert_eq!(facing, rover::Facing::N)
} // }
} // }
#[test] // #[test]
fn spin_right() { // fn spin_right() {
let r = Rover(1, 1, Facing::S); // let r = (1 as i32, 1 as i32, rover::Facing::S);
match r.rotate(Rotation::Right) { // match rover::rotate(&r, rover::Rotation::Right) {
Rover(_, _, facing) => assert_eq!(facing, Facing::W) // (_, _, facing) => assert_eq!(facing, rover::Facing::W)
} // }
} // }
// #[test]
// fn drive() {
// let r = (1 as i32, 1 as i32, rover::Facing::S);
// assert_eq!(driver::execute(&r, "ffrf"),
// (0 as i32, 3 as i32, rover::Facing::W));
// }
#[test]
fn drive() {
let r = Rover(1, 1, Facing::S);
assert_eq!(driver::execute(&r, "ffrf"),
Rover(0, 3, Facing::W));
}
} }