pub fn robot_comparing(&self, m0: Microchip, m1: Microchip) -> Option<Id> {
            // Each microchip follow a similar path. It start with an input bin, then goes through
            // a number of robots comparing it, and finally is given to an output bin. We can
            // represent the "path" that a microchip goes through like this:
            //
            //     input bin → first robot → another robot → another robot → ... → output bin
            //
            // starting with the robot initially holding `m0` (arbitrarily), our goal is to follow
            // its path until we find a robot comparing `m0` with `m1` (our target pair) or its
            // output bin (meaning that no robot is responsible for comparing our target pair).
            let target_pair = Microchip2::new(m0, m1);
            // Find out which robot is taking one of the target microchip from an input bin.
            let first_robot = self.robots.values().find(|&robot| robot.is_initially_holding(m0));
            if first_robot.is_none() {
                return None;
            }
            // memoized hash from robots id to its compared microchips.
            let mut memo: HashMap<Id, Microchip2> = HashMap::new();
            let mut robot = first_robot.unwrap();
            loop {
                let robot_pair = self.compared_microchips(robot, &mut memo);
                if robot_pair == target_pair { // We found it!
                    return Some(robot.id);
                }
                // Here we know that the current robot is responsible for comparing `m0` and some
                // other microchip `c != m1`. Since we know both `m0` and `c` values, we can
                // compare them to "follow" the next robots responsible for comparing `m0`.
                robot = match robot {
                    &Robot { low_to: Output::Robot(next_id), .. } if robot_pair.low == m0 => {
                        self.robots.get(&next_id).unwrap()
                    },
                    &Robot { high_to: Output::Robot(next_id), .. } if robot_pair.high == m0 => {
                        self.robots.get(&next_id).unwrap()
                    },
                    _ => return None, // could be that the next "hop" is an output bin
                }
            }
        }
/// Returns the robot responsible for comparing the microchip pair `(m0, m1)`.
Returns the robot responsible for comparing the microchip pair `(m0, m1)`.
Returns the robot responsible for comparing the microchip pair ` ( m0 m1 ) ` .
pub fn robot_comparing(&self, m0: Microchip, m1: Microchip) -> Option<Id> {
            // Each microchip follow a similar path. It start with an input bin, then goes through
            // a number of robots comparing it, and finally is given to an output bin. We can
            // represent the "path" that a microchip goes through like this:
            //
            //     input bin → first robot → another robot → another robot → ... → output bin
            //
            // starting with the robot initially holding `m0` (arbitrarily), our goal is to follow
            // its path until we find a robot comparing `m0` with `m1` (our target pair) or its
            // output bin (meaning that no robot is responsible for comparing our target pair).
            let target_pair = Microchip2::new(m0, m1);
            // Find out which robot is taking one of the target microchip from an input bin.
            let first_robot = self.robots.values().find(|&robot| robot.is_initially_holding(m0));
            if first_robot.is_none() {
                return None;
            }
            // memoized hash from robots id to its compared microchips.
            let mut memo: HashMap<Id, Microchip2> = HashMap::new();
            let mut robot = first_robot.unwrap();
            loop {
                let robot_pair = self.compared_microchips(robot, &mut memo);
                if robot_pair == target_pair { // We found it!
                    return Some(robot.id);
                }
                // Here we know that the current robot is responsible for comparing `m0` and some
                // other microchip `c != m1`. Since we know both `m0` and `c` values, we can
                // compare them to "follow" the next robots responsible for comparing `m0`.
                robot = match robot {
                    &Robot { low_to: Output::Robot(next_id), .. } if robot_pair.low == m0 => {
                        self.robots.get(&next_id).unwrap()
                    },
                    &Robot { high_to: Output::Robot(next_id), .. } if robot_pair.high == m0 => {
                        self.robots.get(&next_id).unwrap()
                    },
                    _ => return None, // could be that the next "hop" is an output bin
                }
            }
        }
pub fn robot_comparing ( & self , m0 : Microchip , m1 : Microchip ) -> Option < Id > { // Each microchip follow a similar path. It start with an input bin, then goes through // a number of robots comparing it, and finally is given to an output bin. We can // represent the "path" that a microchip goes through like this: // //     input bin → first robot → another robot → another robot → ... → output bin // // starting with the robot initially holding `m0` (arbitrarily), our goal is to follow // its path until we find a robot comparing `m0` with `m1` (our target pair) or its // output bin (meaning that no robot is responsible for comparing our target pair). let target_pair = Microchip2 :: new ( m0 , m1 ) ; // Find out which robot is taking one of the target microchip from an input bin. let first_robot = self . robots . values ( ) . find ( | & robot | robot . is_initially_holding ( m0 ) ) ; if first_robot . is_none ( ) { return None ; } // memoized hash from robots id to its compared microchips. let mut memo : HashMap < Id , Microchip2 > = HashMap :: new ( ) ; let mut robot = first_robot . unwrap ( ) ; loop { let robot_pair = self . compared_microchips ( robot , & mut memo ) ; if robot_pair == target_pair { // We found it! return Some ( robot . id ) ; } // Here we know that the current robot is responsible for comparing `m0` and some // other microchip `c != m1`. Since we know both `m0` and `c` values, we can // compare them to "follow" the next robots responsible for comparing `m0`. robot = match robot { & Robot { low_to : Output :: Robot ( next_id ) , .. } if robot_pair . low == m0 => { self . robots . get ( & next_id ) . unwrap ( ) } , & Robot { high_to : Output :: Robot ( next_id ) , .. } if robot_pair . high == m0 => { self . robots . get ( & next_id ) . unwrap ( ) } , _ => return None , // could be that the next "hop" is an output bin } } }
Returns the robot responsible for comparing the microchip pair `(m0, m1)`.
Returns the robot responsible for comparing the microchip pair ` ( m0 m1 ) ` .
&self
m0
Microchip
m1
Microchip
&self
m0
Microchip
m1
Microchip