1
0

Added day 5 part 2

This commit is contained in:
Ishan Jain 2022-12-05 12:17:31 +05:30
parent fbdcc46626
commit e2e9d95a16
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -93,16 +93,21 @@ fn main() {
fn solution(mut input: Stack) -> String { fn solution(mut input: Stack) -> String {
for ins in input.instructions { for ins in input.instructions {
for _ in 0..ins.count { let l = input.stacks[ins.from - 1].len();
if let Some(v) = input.stacks[ins.from - 1].pop() {
input.stacks[ins.to - 1].push(v); let crates: Vec<char> = input.stacks[ins.from - 1]
} .drain(l.saturating_sub(ins.count)..l)
.collect();
for c in crates {
input.stacks[ins.to - 1].push(c);
} }
} }
input input
.stacks .stacks
.into_iter() .into_iter()
.filter(|c| !c.is_empty())
.map(|c| *c.last().unwrap()) .map(|c| *c.last().unwrap())
.collect() .collect()
} }