From 1ced7166625cc8f272131202f6dbb77463e38682 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Sat, 31 Oct 2020 18:44:23 +0100 Subject: [PATCH 1/4] Robot need to piouff some time :) --- src/robot.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) 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)); + } } From afd8b1dc8b5287228cbcd01d2a154533a862dc9e Mon Sep 17 00:00:00 2001 From: Martin HART Date: Sat, 31 Oct 2020 18:50:29 +0100 Subject: [PATCH 2/4] need to fix a nasty bug... --- src/main.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main.rs b/src/main.rs index e9e1c5e..3733ac9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -218,6 +218,19 @@ fn main() -> Result<(), Box> { let mut hash: HashMap = HashMap::new(); create_hash_map(&robot_pool, &mut hash); + let mut piouff: u32 = 0; + while piouff != robot_pool.len() as u32 { + for r in robot_pool { + if robot::is_piouff(&r) { + piouff += 1; + } else { + r.execute_instruction(); + } + check_map(&r, &world)?; + check_collisions(&r, &hash)?; + } + } + Ok(()) } From a470e883af95205b9faf3702f750bf6a661f5306 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Sat, 31 Oct 2020 19:23:04 +0100 Subject: [PATCH 3/4] fix iteration bug --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3733ac9..211ab08 100644 --- a/src/main.rs +++ b/src/main.rs @@ -218,9 +218,9 @@ fn main() -> Result<(), Box> { let mut hash: HashMap = HashMap::new(); create_hash_map(&robot_pool, &mut hash); - let mut piouff: u32 = 0; - while piouff != robot_pool.len() as u32 { - for r in robot_pool { + loop { + let mut piouff: u32 = 0; + for mut r in robot_pool { if robot::is_piouff(&r) { piouff += 1; } else { @@ -229,6 +229,9 @@ fn main() -> Result<(), Box> { check_map(&r, &world)?; check_collisions(&r, &hash)?; } + if piouff == robot_pool.len() as u32 { + break; + } } Ok(()) From 60a721742a8352ae135bf3d179d1660e401b6125 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sat, 31 Oct 2020 20:21:21 +0100 Subject: [PATCH 4/4] working main-loop --- src/main.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 211ab08..61e9d8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -220,19 +220,24 @@ fn main() -> Result<(), Box> { loop { let mut piouff: u32 = 0; - for mut r in robot_pool { + 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); } - check_map(&r, &world)?; - check_collisions(&r, &hash)?; } 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(()) }