add S W E

This commit is contained in:
Daniel Barlow 2023-10-31 23:27:07 +00:00
parent 5a148a2688
commit 2b65145621

View File

@ -9,7 +9,13 @@ mod rover {
type Rover = (i32, i32, Facing);
pub fn forward((lon, lat, direction) : Rover) -> Rover {
(lon as i32, lat - 1 as i32, direction)
let (xoff, yoff) = match direction {
Facing::N => ( 0, -1),
Facing::E => ( 1, 0),
Facing::S => ( 0, 1),
Facing::W => (-1, 0)
};
(lon + xoff as i32, lat + yoff as i32, direction)
}
}
@ -27,4 +33,25 @@ mod tests {
assert_eq!(rover::forward(r),
(1 as i32, 2 as i32, 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_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));
}
}