Day 5, puzzle 2

This commit is contained in:
jazzpi 2022-12-13 16:41:53 +01:00
parent a4299a1fc5
commit 0506dc670a
1 changed files with 34 additions and 0 deletions

34
src/bin/d5p2.rs Normal file
View File

@ -0,0 +1,34 @@
use std::collections::VecDeque;
use aoc22::{day5, util};
pub fn main() {
let mut arrangement = day5::parse_arrangement(&util::parse_input());
for inst in &arrangement.instructions {
let mut boxes = VecDeque::new();
for _ in 0..inst.num {
let box_ = arrangement
.stacks
.get_mut(inst.from)
.unwrap()
.pop_back()
.unwrap();
boxes.push_front(box_);
}
arrangement
.stacks
.get_mut(inst.to)
.unwrap()
.append(&mut boxes);
}
let top_boxes: String = arrangement
.stacks
.iter()
.map(|s| s.iter().next_back().unwrap())
.collect();
println!("The top boxes are {}", top_boxes);
}