Compare commits

..

6 Commits
wip ... main

View File

@ -20,6 +20,15 @@ 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;
@ -33,119 +42,112 @@ mod rover {
Rover(lon - xoff, lat - yoff, *direction) Rover(lon - xoff, lat - yoff, *direction)
} }
// fn next_direction(direction : Facing) -> Facing { pub fn rotate(&self, rotation: Rotation) -> Rover {
// match direction { let Rover(lon, lat, direction) = self;
// Facing::N => Facing::W, let new_direction = match rotation {
// Facing::W => Facing::S, Rotation::Left => {
// Facing::S => Facing::E, next_direction(*direction)
// Facing::E => Facing::N, },
// } Rotation::Right => {
// } next_direction(
next_direction(
// pub fn rotate((lon, lat, direction) : &Rover, rotation: Rotation) -> Rover { next_direction(*direction)
// 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(&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()
// } }
// } }
// 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::*; use super::driver;
use super::rover::{Rover, Facing,Rotation};
#[test] #[test]
fn move_north() { fn move_north() {
let r = rover::Rover(1, 1, rover::Facing::N); let r = Rover(1, 1, Facing::N);
assert_eq!(r.forward(), assert_eq!(r.forward(),
rover::Rover(1, 0, rover::Facing::N)); Rover(1, 0, Facing::N));
let r = rover::Rover(1, 3, rover::Facing::N); let r = Rover(1, 3, Facing::N);
assert_eq!(r.forward(), assert_eq!(r.forward(),
rover::Rover(1, 2, 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 = rover::Rover(1 as i32, 1 as i32, rover::Facing::E); let r = Rover(1, 1, 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 = (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]
// 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));
}
} }