/// Compute the final point and the first point visited twice after having completely
/// followed the given `RecruitingDocument` instructions.
///
/// return a tuple `t` with two values: `t.0` is the final `Point` and `t.1` the optional
/// first `Point` visited twice.
// NOTE: This method does not update the state of self, the puzzle description clearly
// state that we don't have the time to actually _perform_ the instructions: we only need
// to _find_ the Easter Bunny Headquarters position(s) in order to compute the distance(s).
Compute the final point and the first point visited twice after having completely
followed the given `RecruitingDocument` instructions.


Compute the final point and the first point visited twice after having completely followed the given ` RecruitingDocument ` instructions .
pub fn follow(&self, document: &RecruitingDocument) -> (Point, Option<Point>) {
            let (mut position, mut direction) = (self.position, document.initial_direction);
            let mut visited = HashSet::new();
            visited.insert(position);
            let mut first_position_visited_twice = None;
            for instruction in &document.instructions {
                match *instruction {
                    Instruction::TurnRight => {
                        direction = match direction {
                            Direction::North => Direction::East,
                            Direction::East  => Direction::South,
                            Direction::South => Direction::West,
                            Direction::West  => Direction::North,
                        }
                    }
                    Instruction::TurnLeft => {
                        direction = match direction {
                            Direction::North => Direction::West,
                            Direction::East  => Direction::North,
                            Direction::South => Direction::East,
                            Direction::West  => Direction::South,
                        }
                    }
                    Instruction::Walk(count) => {
                        for _ in 0..count {
                            position = match direction {
                                Direction::North => Point { y: position.y + 1, ..position },
                                Direction::East  => Point { x: position.x + 1, ..position },
                                Direction::South => Point { y: position.y - 1, ..position },
                                Direction::West  => Point { x: position.x - 1, ..position },
                            };
                            if first_position_visited_twice.is_none() && !visited.insert(position) {
                                first_position_visited_twice = Some(position);
                            }
                        }
                    }
                }
            }
            (position, first_position_visited_twice)
        }
pub fn follow ( & self , document : & RecruitingDocument ) -> ( Point , Option < Point > ) { let ( mut position , mut direction ) = ( self . position , document . initial_direction ) ; let mut visited = HashSet :: new ( ) ; visited . insert ( position ) ; let mut first_position_visited_twice = None ; for instruction in & document . instructions { match * instruction { Instruction :: TurnRight => { direction = match direction { Direction :: North => Direction :: East , Direction :: East => Direction :: South , Direction :: South => Direction :: West , Direction :: West => Direction :: North , } } Instruction :: TurnLeft => { direction = match direction { Direction :: North => Direction :: West , Direction :: East => Direction :: North , Direction :: South => Direction :: East , Direction :: West => Direction :: South , } } Instruction :: Walk ( count ) => { for _ in 0 .. count { position = match direction { Direction :: North => Point { y : position . y + 1 , .. position } , Direction :: East => Point { x : position . x + 1 , .. position } , Direction :: South => Point { y : position . y - 1 , .. position } , Direction :: West => Point { x : position . x - 1 , .. position } , } ; if first_position_visited_twice . is_none ( ) && ! visited . insert ( position ) { first_position_visited_twice = Some ( position ) ; } } } } } ( position , first_position_visited_twice ) }
Compute the final point and the first point visited twice after having completely
followed the given `RecruitingDocument` instructions.
Compute the final point and the first point visited twice after having completely followed the given ` RecruitingDocument ` instructions .
&self
document
&RecruitingDocument