make Rover a struct instead of a raw tuple

This commit is contained in:
Daniel Barlow 2023-11-03 18:41:40 +00:00
parent 023923f06b
commit d2f123bcf6

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,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); 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); 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 { fn next_direction(direction : Facing) -> Facing {
@ -36,19 +39,21 @@ mod rover {
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(slf : &Rover, rotation: Rotation) -> Rover {
let Rover(lon, lat, direction) = slf;
match rotation { match rotation {
Rotation::Left => { Rotation::Left => {
let new_d = next_direction(*direction); let new_d = next_direction(*direction);
(*lon, *lat, new_d) Rover(*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) Rover(*lon, *lat, new_d)
} }
} }
} }
@ -80,67 +85,67 @@ mod driver {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::{rover, driver};
use super::rover::{Rover};
#[test] #[test]
fn move_north() { 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), 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), 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] #[test]
fn move_south() { 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), 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] #[test]
fn move_west() { 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), 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] #[test]
fn move_east() { 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), 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] #[test]
fn move_backward() { 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); let r2 = rover::forward(&r);
assert_eq!(rover::backward(&r2), r); assert_eq!(rover::backward(&r2), r);
} }
#[test] #[test]
fn spin_left() { 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) { match rover::rotate(&r, rover::Rotation::Left) {
(_, _, facing) => assert_eq!(facing, rover::Facing::N) Rover(_, _, 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 = Rover(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) Rover(_, _, 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 = Rover(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)); Rover(0 as i32, 3 as i32, rover::Facing::W));
} }
} }