diff --git a/rust/day5/input b/rust/day5/input index e054278..1256625 100644 --- a/rust/day5/input +++ b/rust/day5/input @@ -508,5 +508,4 @@ move 9 from 4 to 5 move 2 from 8 to 2 move 4 from 6 to 9 move 3 from 2 to 4 -move 1 from 8 to 6 - +move 1 from 8 to 6 \ No newline at end of file diff --git a/rust/day5/src/main.rs b/rust/day5/src/main.rs index 4fb85be..cb7827f 100644 --- a/rust/day5/src/main.rs +++ b/rust/day5/src/main.rs @@ -3,19 +3,20 @@ use std::fs::read_to_string; 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 commands_tuple: Vec<(u32, u32, u32)> = Vec::new(); // Spliting the str into a cmd part, and crates part. - for (i, txt) in info.iter().enumerate() { - if txt.len() == 0 { - info.remove(i); - (crates, cmd) = info.split_at(i); - break; - } - } + let split_index = info + .iter() + .enumerate() + .find(|txt| txt.1.len() == 0) + .unwrap() + .to_owned() + .0; + info.remove(split_index); + let (crates, cmd) = info.split_at(split_index); + // Gathering the horizontal number of crates let max_crates: Vec = crates .last() @@ -24,18 +25,19 @@ fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec>) { .split_whitespace() .map(|str| str.parse().unwrap()) .collect(); + let max = max_crates.iter().max().unwrap().to_owned(); // Gathering each crate Letters - for _i in 0..max_crates.iter().max().unwrap().to_owned() { + for _i in 0..max { 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) { + for y in 0..max { + let ch = str.as_bytes()[((y * 4) + 1) as usize]; + if ch != (' ' as u8) { crates_str[y as usize].push(ch as char); } } @@ -58,13 +60,38 @@ fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec>) { // Returning every info as a tuple ( - max_crates.iter().max().unwrap().to_owned(), + max, commands_tuple.to_owned(), crates_str.to_owned(), ) } + +fn first_part(cmd: &Vec<(u32, u32, u32)>, crates: &Vec>) -> String +{ + // Algorithm + let mut boxes = crates.clone(); + for operation in cmd { + let mut moving_crates: Vec = Vec::new(); + for _ in 0..(operation.0) { + moving_crates.push(boxes[(operation.1-1) as usize].last().unwrap().to_owned()); + boxes[(operation.1-1) as usize].pop(); + } + for mv in moving_crates { + boxes[(operation.2-1) as usize].push(mv); + } + } + + // Returning chars + let mut return_chars: Vec = Vec::new(); + for b in boxes { + return_chars.push(b.last().unwrap().to_owned()); + } + return_chars.into_iter().collect() +} + fn main() { - let file_content = read_to_string("exemple").unwrap(); + let file_content = read_to_string("input").unwrap(); let (max, cmd, crates) = analyse(&file_content); println!("max = {}\ncmd = {:?}\ncrates = {:?}", max, cmd, crates); + println!("First part: {:?}", first_part(&cmd, &crates)); }