AoC/2022/days/05/supply-stacks/src/main.rs

42 lines
1.3 KiB
Rust
Executable File

//! # Day 5: Supply Stacks
use std::collections::VecDeque;
fn extract_crates_in_layer(layer: &str, piles_number: usize) -> Vec<char> {
let mut crates: Vec<char> = Vec::with_capacity(piles_number);
for i in 0..piles_number {
let crate_name_idx: usize = i * 4 + 1;
let crate_name: char = layer.chars().nth(crate_name_idx).unwrap();
crates[i] = crate_name;
}
crates
}
fn parse_crates_piles(entrepot: &str) -> Vec<VecDeque<char>> {
let mut piles: Vec<VecDeque<char>> = Vec::new();
let mut number_of_piles: usize = 0;
let mut entrepot_layers = entrepot.lines();
let last_line = entrepot_layers.nth_back(0);
let piles_number: usize = (last_line.expect("reason").len() + 1) / 4 as usize;
for pile_idx in 0..piles_number {
piles[pile_idx] = VecDeque::new();
}
let j = 0;
let number_of_layers: usize = entrepot_layers.clone().count();
for layer in entrepot_layers {
if j != number_of_layers - 1 {
// Not the last line
let crates = extract_crates_in_layer(layer, piles_number);
// Enqueue below the pile
for pile_idx in 0..crates.len() {
piles[pile_idx].push_back(crates[pile_idx]);
}
}
}
piles
}
fn main() {
let input_file = "../data/sample.txt";
}