added hash in execute_instruction
This commit is contained in:
parent
466a257300
commit
0931835850
@ -9,11 +9,15 @@ pub struct Robot {
|
|||||||
|
|
||||||
impl Robot {
|
impl Robot {
|
||||||
/// Create new `Robot` with given id, `Orientation`, `Position` and instructions.
|
/// Create new `Robot` with given id, `Orientation`, `Position` and instructions.
|
||||||
fn new(id: i32, o: Orientation, p: Position, i: Vec<char>) -> Robot {
|
fn new(id: u32, o: Orientation, p: Position, i: Vec<char>) -> Robot {
|
||||||
Robot { id, o, p, i }
|
Robot { id, o, p, i }
|
||||||
}
|
}
|
||||||
/// Apply given instruction to a `Robot`.
|
/// Apply given instruction to a `Robot`.
|
||||||
fn execute_instruction(&mut self) -> Result<(), &'static str> {
|
fn execute_instruction(
|
||||||
|
&mut self,
|
||||||
|
hash: &mut std::collections::HashMap<Position, u32>,
|
||||||
|
) -> Result<(), &'static str> {
|
||||||
|
hash.remove(&self.p); // we need to insert the new position after calling execute_instruction()
|
||||||
match self.i.pop() {
|
match self.i.pop() {
|
||||||
Some(instruction) => match instruction {
|
Some(instruction) => match instruction {
|
||||||
'L' => Ok(match self.o {
|
'L' => Ok(match self.o {
|
||||||
@ -50,6 +54,7 @@ enum Orientation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Struct to store robot position.
|
/// Struct to store robot position.
|
||||||
|
#[derive(PartialEq, Eq, Hash)]
|
||||||
struct Position {
|
struct Position {
|
||||||
x: i32,
|
x: i32,
|
||||||
y: i32,
|
y: i32,
|
||||||
|
Loading…
Reference in New Issue
Block a user