1
0
Fork 0

analyse() now returns the correct values for the crates vector.

This commit is contained in:
Yohan Boujon 2023-10-15 17:15:49 +02:00
parent b94278d500
commit a823166659

View file

@ -1,11 +1,11 @@
use std::fs::read_to_string;
fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec<Vec<&str>>) {
fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec<Vec<char>>) {
// 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<&str>> = Vec::new();
let mut crates_str: Vec<Vec<char>> = 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<Vec<&str>>) {
.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<Option<u32>> = 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<Option<u32>> = str
.split_whitespace()
.collect::<Vec<&str>>()
.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() {