From c9ad3107007d4172ec403bfd15f54686986fb613 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Wed, 28 Oct 2020 17:30:45 +0100 Subject: [PATCH 1/5] create and move robot + tests --- src/robot.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/src/robot.rs b/src/robot.rs index a9d6e6a..da57e61 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -1,9 +1,44 @@ /// A Robot *aka droid* is represented here. /// Each robot must have a unique id. pub struct Robot { - id: u32, + id: i32, o: Orientation, p: Position, + i: Vec, +} + +impl Robot { + /// Create new `Robot` with given id, `Orientation`, `Position` and instructions. + fn new(id: i32, o: Orientation, p: Position, i: Vec) -> Robot { + Robot { id, o, p, i } + } + /// Apply given instruction to a `Robot`. + fn execute_instruction(&mut self) -> Result<(), &'static str> { + match self.i.pop() { + Some(instruction) => match instruction { + 'L' => Ok(match self.o { + Orientation::N => self.o = Orientation::W, + Orientation::E => self.o = Orientation::N, + Orientation::S => self.o = Orientation::E, + Orientation::W => self.o = Orientation::S, + }), + 'R' => Ok(match self.o { + Orientation::N => self.o = Orientation::E, + Orientation::E => self.o = Orientation::S, + Orientation::S => self.o = Orientation::W, + Orientation::W => self.o = Orientation::N, + }), + 'F' => Ok(match self.o { + Orientation::N => self.p.y += 1, + Orientation::E => self.p.x += 1, + Orientation::S => self.p.y -= 1, + Orientation::W => self.p.x -= 1, + }), + _ => Err("Invalid instruction."), + }, + None => Ok(()), + } + } } /// Enum to store all possible orientations. @@ -19,3 +54,36 @@ struct Position { x: i32, y: i32, } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_new_robot() { + let r: Robot = Robot::new( + 0, + Orientation::N, + Position { x: 1, y: 2 }, + vec!['L', 'L', 'F', 'R'], + ); + assert_eq!(r.id, 0); + assert!(matches!(r.o, Orientation::N)); + assert_eq!(r.p.x, 1); + assert_eq!(r.p.y, 2); + assert_eq!(r.i, vec!['L', 'L', 'F', 'R']); + } + + #[test] + fn test_execute_instruction() { + let mut r: Robot = Robot::new( + 0, + Orientation::N, + Position { x: 1, y: 2 }, + vec!['R', 'L', 'F', 'F'], + ); + assert!(r.execute_instruction().is_ok()); + assert_eq!(r.p.x, 1); + assert_eq!(r.p.y, 3); + } +} From 466a257300233e41435e6fb800766492d59c623a Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Wed, 28 Oct 2020 17:32:36 +0100 Subject: [PATCH 2/5] oups --- src/robot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/robot.rs b/src/robot.rs index da57e61..59fa0bd 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -1,7 +1,7 @@ /// A Robot *aka droid* is represented here. /// Each robot must have a unique id. pub struct Robot { - id: i32, + id: u32, o: Orientation, p: Position, i: Vec, From 0931835850fbc85357a27b9a6cd05b9a0abbf09e Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Wed, 28 Oct 2020 18:20:37 +0100 Subject: [PATCH 3/5] added hash in execute_instruction --- src/robot.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/robot.rs b/src/robot.rs index 59fa0bd..09901c4 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -9,11 +9,15 @@ pub struct Robot { impl Robot { /// Create new `Robot` with given id, `Orientation`, `Position` and instructions. - fn new(id: i32, o: Orientation, p: Position, i: Vec) -> Robot { + fn new(id: u32, o: Orientation, p: Position, i: Vec) -> Robot { Robot { id, o, p, i } } /// Apply given instruction to a `Robot`. - fn execute_instruction(&mut self) -> Result<(), &'static str> { + fn execute_instruction( + &mut self, + hash: &mut std::collections::HashMap, + ) -> Result<(), &'static str> { + hash.remove(&self.p); // we need to insert the new position after calling execute_instruction() match self.i.pop() { Some(instruction) => match instruction { 'L' => Ok(match self.o { @@ -50,6 +54,7 @@ enum Orientation { } /// Struct to store robot position. +#[derive(PartialEq, Eq, Hash)] struct Position { x: i32, y: i32, From b8929887b52f78beea96bf1d591f98a0d11b1437 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Wed, 28 Oct 2020 19:12:30 +0100 Subject: [PATCH 4/5] omg... --- src/robot.rs | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/robot.rs b/src/robot.rs index 09901c4..b40ec1e 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -13,34 +13,30 @@ impl Robot { Robot { id, o, p, i } } /// Apply given instruction to a `Robot`. - fn execute_instruction( - &mut self, - hash: &mut std::collections::HashMap, - ) -> Result<(), &'static str> { - hash.remove(&self.p); // we need to insert the new position after calling execute_instruction() + fn execute_instruction(&mut self) { match self.i.pop() { Some(instruction) => match instruction { - 'L' => Ok(match self.o { + 'L' => match self.o { Orientation::N => self.o = Orientation::W, Orientation::E => self.o = Orientation::N, Orientation::S => self.o = Orientation::E, Orientation::W => self.o = Orientation::S, - }), - 'R' => Ok(match self.o { + }, + 'R' => match self.o { Orientation::N => self.o = Orientation::E, Orientation::E => self.o = Orientation::S, Orientation::S => self.o = Orientation::W, Orientation::W => self.o = Orientation::N, - }), - 'F' => Ok(match self.o { + }, + 'F' => match self.o { Orientation::N => self.p.y += 1, Orientation::E => self.p.x += 1, Orientation::S => self.p.y -= 1, Orientation::W => self.p.x -= 1, - }), - _ => Err("Invalid instruction."), + }, + _ => (), }, - None => Ok(()), + None => (), } } } @@ -85,10 +81,18 @@ mod tests { 0, Orientation::N, Position { x: 1, y: 2 }, - vec!['R', 'L', 'F', 'F'], + vec!['R', 'F', 'L', 'F'], ); - assert!(r.execute_instruction().is_ok()); + let mut hash = std::collections::HashMap::new(); + //hash.insert(&r.p, &r.id); // first insert while initializing. + hash.remove(&r.p); // remove before execute_instruction(). + r.execute_instruction(); + hash.insert(&r.p, &r.id); // second insert after moving. assert_eq!(r.p.x, 1); assert_eq!(r.p.y, 3); + r.execute_instruction(); + r.execute_instruction(); + assert_eq!(r.p.x, 0); + assert_eq!(r.p.y, 3); } } From cd15d12549ae3caaa000d88744138e46cb2d8ce5 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Wed, 28 Oct 2020 19:18:31 +0100 Subject: [PATCH 5/5] comment --- src/robot.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/robot.rs b/src/robot.rs index b40ec1e..bedefa3 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -34,7 +34,7 @@ impl Robot { Orientation::S => self.p.y -= 1, Orientation::W => self.p.x -= 1, }, - _ => (), + _ => (), // never happens 😉 }, None => (), }