From cd8f4319ab74c32d7df07f56cdbeb4640d09371f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Thu, 30 Jun 2022 19:03:02 +0200 Subject: [PATCH] Completed solution. --- src/main.rs | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index cd35bef..54f00fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn main() { let mut boxes:Vec = Vec::with_capacity(size as usize); { - let mut nums:Vec = (1..size+1).collect(); + let mut nums:Vec = (0..size).collect(); while nums.len() > 0 { @@ -38,4 +38,40 @@ fn main() { nums.remove(rand_i); } } + + let mut all_found = true; + for i in 0..size + { + let mut next_box:usize = i as usize; + let mut found = false; + for _j in 0..(size/2) + { + if boxes[next_box] == i + { + found = true; + break; + } + else + { + next_box = boxes[next_box] as usize; + if next_box == i as usize + { + break; + } + } + } + if !found + { + all_found = false; + } + } + + if all_found + { + println!("All prisoners found their numbers!"); + } + else + { + println!("Some prisoners didn't find their numbers!"); + } }