1
0
Fork 0

Day 5 Part 1.

This commit is contained in:
Yohan Boujon 2023-10-15 18:13:18 +02:00
parent a823166659
commit b6c1733c21
2 changed files with 43 additions and 17 deletions

View file

@ -508,5 +508,4 @@ move 9 from 4 to 5
move 2 from 8 to 2 move 2 from 8 to 2
move 4 from 6 to 9 move 4 from 6 to 9
move 3 from 2 to 4 move 3 from 2 to 4
move 1 from 8 to 6 move 1 from 8 to 6

View file

@ -3,19 +3,20 @@ use std::fs::read_to_string;
fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec<Vec<char>>) { fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec<Vec<char>>) {
// Initializing some values. // Initializing some values.
let mut info: Vec<_> = str.split("\r\n").collect(); let mut info: Vec<_> = str.split("\r\n").collect();
let mut crates: &[&str] = &[""];
let mut cmd: &[&str] = &[""];
let mut crates_str: Vec<Vec<char>> = Vec::new(); let mut crates_str: Vec<Vec<char>> = Vec::new();
let mut commands_tuple: Vec<(u32, u32, u32)> = Vec::new(); let mut commands_tuple: Vec<(u32, u32, u32)> = Vec::new();
// Spliting the str into a cmd part, and crates part. // Spliting the str into a cmd part, and crates part.
for (i, txt) in info.iter().enumerate() { let split_index = info
if txt.len() == 0 { .iter()
info.remove(i); .enumerate()
(crates, cmd) = info.split_at(i); .find(|txt| txt.1.len() == 0)
break; .unwrap()
} .to_owned()
} .0;
info.remove(split_index);
let (crates, cmd) = info.split_at(split_index);
// Gathering the horizontal number of crates // Gathering the horizontal number of crates
let max_crates: Vec<u32> = crates let max_crates: Vec<u32> = crates
.last() .last()
@ -24,18 +25,19 @@ fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec<Vec<char>>) {
.split_whitespace() .split_whitespace()
.map(|str| str.parse().unwrap()) .map(|str| str.parse().unwrap())
.collect(); .collect();
let max = max_crates.iter().max().unwrap().to_owned();
// Gathering each crate Letters // 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()); crates_str.push(Vec::new());
} }
for (i, str) in crates.iter().rev().enumerate() { for (i, str) in crates.iter().rev().enumerate() {
if i == 0 { if i == 0 {
continue; continue;
} }
for y in 0..max_crates.iter().max().unwrap().to_owned() { for y in 0..max {
let ch = str.as_bytes()[((y*4)+1) as usize]; let ch = str.as_bytes()[((y * 4) + 1) as usize];
if ch != (' ' as u8) { if ch != (' ' as u8) {
crates_str[y as usize].push(ch as char); crates_str[y as usize].push(ch as char);
} }
} }
@ -58,13 +60,38 @@ fn analyse(str: &String) -> (u32, Vec<(u32, u32, u32)>, Vec<Vec<char>>) {
// Returning every info as a tuple // Returning every info as a tuple
( (
max_crates.iter().max().unwrap().to_owned(), max,
commands_tuple.to_owned(), commands_tuple.to_owned(),
crates_str.to_owned(), crates_str.to_owned(),
) )
} }
fn first_part(cmd: &Vec<(u32, u32, u32)>, crates: &Vec<Vec<char>>) -> String
{
// Algorithm
let mut boxes = crates.clone();
for operation in cmd {
let mut moving_crates: Vec<char> = 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<char> = Vec::new();
for b in boxes {
return_chars.push(b.last().unwrap().to_owned());
}
return_chars.into_iter().collect()
}
fn main() { 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); let (max, cmd, crates) = analyse(&file_content);
println!("max = {}\ncmd = {:?}\ncrates = {:?}", max, cmd, crates); println!("max = {}\ncmd = {:?}\ncrates = {:?}", max, cmd, crates);
println!("First part: {:?}", first_part(&cmd, &crates));
} }