From 15d7b18af8bbe8088741828a6624eec93647b116 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 2 Nov 2023 23:44:48 +0000 Subject: [PATCH] add methods to Rover --- src/main.rs | 199 +++++++++++++++++++++++++++------------------------- 1 file changed, 102 insertions(+), 97 deletions(-) diff --git a/src/main.rs b/src/main.rs index 013de76..196a86b 100644 --- a/src/main.rs +++ b/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,64 +20,68 @@ 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, - Facing::W => Facing::S, - Facing::S => Facing::E, - Facing::E => Facing::N, + 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 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) - } + pub fn backward(&self) -> Rover { + let Rover(lon, lat, direction) = self; + let (xoff, yoff) = velocity(&direction); + Rover(lon - xoff, lat - yoff, *direction) } + + // fn next_direction(direction : Facing) -> Facing { + // match direction { + // Facing::N => Facing::W, + // Facing::W => Facing::S, + // Facing::S => Facing::E, + // Facing::E => Facing::N, + // } + // } + + // 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) + // } + // } + // } } } -mod driver { - use super::rover; +// mod driver { +// use super::rover; - 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), - _ => rover.clone() - } - } +// 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), +// _ => rover.clone() +// } +// } - pub fn execute(rover: &rover::Rover, commands: &str) -> rover::Rover { - if commands.is_empty() { - rover.clone() - } else { - let (now, later) = commands.split_at(1); - execute(&perform_command(now, rover), later) - } - } -} +// pub fn execute(rover: &rover::Rover, commands: &str) -> rover::Rover { +// if commands.is_empty() { +// rover.clone() +// } else { +// let (now, later) = commands.split_at(1); +// execute(&perform_command(now, rover), later) +// } +// } +// } #[cfg(test)] mod tests { @@ -84,63 +89,63 @@ mod tests { #[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::Rover(1, 1, rover::Facing::N); + assert_eq!(r.forward(), + rover::Rover(1, 0, rover::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::Rover(1, 3, rover::Facing::N); + assert_eq!(r.forward(), + rover::Rover(1, 2, rover::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)); - } + // #[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)); + // } - #[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)); - } + // #[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)); + // } - #[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)); - } + // #[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)); + // } #[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::Rover(1 as i32, 1 as i32, rover::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) - } - } - #[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) - } - } + // #[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) + // } + // } + // #[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) + // } + // } - #[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 = (1 as i32, 1 as i32, rover::Facing::S); + // assert_eq!(driver::execute(&r, "ffrf"), + // (0 as i32, 3 as i32, rover::Facing::W)); + // } }