From a823166659f695b5de78c1eb80bb6a855eed0ec6 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 15 Oct 2023 17:15:49 +0200 Subject: [PATCH] analyse() now returns the correct values for the crates vector. --- rust/day5/src/main.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/rust/day5/src/main.rs b/rust/day5/src/main.rs index 8ec547b..4fb85be 100644 --- a/rust/day5/src/main.rs +++ b/rust/day5/src/main.rs @@ -1,11 +1,11 @@ use std::fs::read_to_string; -fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec>) { +fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec>) { // Initializing some values. let mut info: Vec<_> = str.split("\r\n").collect(); let mut crates: &[&str] = &[""]; let mut cmd: &[&str] = &[""]; - let mut crates_str: Vec> = Vec::new(); + let mut crates_str: Vec> = Vec::new(); let mut commands_tuple: Vec<(u32, u32, u32)> = Vec::new(); // Spliting the str into a cmd part, and crates part. @@ -26,25 +26,41 @@ fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec>) { .collect(); // Gathering each crate Letters - for (i, str) in crates.iter().enumerate() { - if i == crates.len()-1 { - break; + for _i in 0..max_crates.iter().max().unwrap().to_owned() { + crates_str.push(Vec::new()); + } + for (i, str) in crates.iter().rev().enumerate() { + if i == 0 { + continue; + } + for y in 0..max_crates.iter().max().unwrap().to_owned() { + let ch = str.as_bytes()[((y*4)+1) as usize]; + if ch != (' ' as u8) { + crates_str[y as usize].push(ch as char); + } } - crates_str.push(str.split_whitespace().collect()); } // Rearanging commands as simplier datatypes to process. for str in cmd { - let str_temp : Vec<&str> = str.split_whitespace().collect(); - let tuple_temp : Vec> = str_temp.iter().map_while(|str| Some(str.parse().ok())).collect(); - commands_tuple.push((tuple_temp[1].unwrap(), tuple_temp[3].unwrap(), tuple_temp[5].unwrap())); + let tuple_temp: Vec> = str + .split_whitespace() + .collect::>() + .iter() + .map_while(|str| Some(str.parse().ok())) + .collect(); + commands_tuple.push(( + tuple_temp[1].unwrap(), + tuple_temp[3].unwrap(), + tuple_temp[5].unwrap(), + )); } // Returning every info as a tuple ( max_crates.iter().max().unwrap().to_owned(), commands_tuple.to_owned(), - crates_str.to_owned() + crates_str.to_owned(), ) } fn main() {