diff --git a/src/main.rs b/src/main.rs index e9e1c5e..61e9d8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -218,6 +218,27 @@ fn main() -> Result<(), Box> { let mut hash: HashMap = HashMap::new(); create_hash_map(&robot_pool, &mut hash); + loop { + let mut piouff: u32 = 0; + for r in &mut robot_pool { + if robot::is_piouff(&r) { + piouff += 1; + } else { + hash.remove(&r.p); + r.execute_instruction(); + check_map(&r, &world)?; + check_collisions(&r, &hash)?; + hash.insert(robot::Position { x: r.p.x, y: r.p.y }, r.id); + } + } + if piouff == robot_pool.len() as u32 { + break; + } + } + for r in &robot_pool { + println!("Robot id: {}: Final position: ({}, {})", r.id, r.p.x, r.p.y); + } + Ok(()) } diff --git a/src/robot.rs b/src/robot.rs index 7ab8c5a..25edfc3 100644 --- a/src/robot.rs +++ b/src/robot.rs @@ -69,6 +69,14 @@ pub fn is_instructions(v: &Vec) -> bool { true } +/// Check if a robot is piouff. +pub fn is_piouff(r: &Robot) -> bool { + if r.i.len() == 0 { + return true; + } + false +} + #[cfg(test)] mod tests { use super::*; @@ -121,4 +129,19 @@ mod tests { let v = vec!['F', 'R', 'L', 'Z']; assert!(is_instructions(&v)); } + + #[test] + fn test_piouf() { + let mut r: Robot = Robot::new( + 0, + Orientation::N, + Position { x: 1, y: 2 }, + vec!['R', 'F', 'L', 'F'], + ); + r.i.pop(); + r.i.pop(); + r.i.pop(); + r.i.pop(); + assert!(is_piouff(&r)); + } }