From 5779749423efe5622d62655021ee1c92e8675052 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 5 Dec 2022 16:46:17 -0800 Subject: [PATCH] done with part 2 --- 2022-aoc/src/d5.rs | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/2022-aoc/src/d5.rs b/2022-aoc/src/d5.rs index 3875b71..10cf42f 100644 --- a/2022-aoc/src/d5.rs +++ b/2022-aoc/src/d5.rs @@ -2,7 +2,7 @@ use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; use itertools::Itertools; type Stax = Vec>; -type Inst = (u8, u8, u8); +type Inst = (usize, usize, usize); enum Pstate { Setup, @@ -42,8 +42,8 @@ fn parse_input_day1(input: &str) -> (Stax, Vec) { let ins = re.captures(line).unwrap(); inst.push(( ins[1].parse().unwrap(), - ins[2].parse::().unwrap() - 1, - ins[3].parse::().unwrap() - 1, + ins[2].parse::().unwrap() - 1, + ins[3].parse::().unwrap() - 1, )); } } @@ -59,14 +59,13 @@ fn part1((stax, inst): &(Stax, Vec)) -> String { let mut stax = stax.to_owned(); for ins in inst.iter() { let (amt, from, to) = ins; - let mut from = &mut stax[*from as usize]; - let mut tmp = Vec::with_capacity(*amt as usize); + let from = &mut stax[*from]; + let mut tmp = Vec::with_capacity(*amt); for _ in 0..*amt { - let from = &mut from; let t = from.pop().unwrap(); tmp.push(t); } - stax[*to as usize].append(&mut tmp); + stax[*to].append(&mut tmp); } let mut s = String::with_capacity(9); for stack in stax { @@ -76,7 +75,23 @@ fn part1((stax, inst): &(Stax, Vec)) -> String { } #[aoc_run(day5, part2)] -fn p2((stax, inst): &(Stax, Vec)) -> u32 { - // - 0 +fn p2((stax, inst): &(Stax, Vec)) -> String { + let mut stax = stax.to_owned(); + for ins in inst.iter() { + let (amt, from, to) = ins; + let from = &mut stax[*from]; + let mut tmp = Vec::with_capacity(*amt); + for _ in 0..*amt { + let c = from.pop().unwrap(); + tmp.push(c); + } + for c in tmp.into_iter().rev() { + stax[*to].push(c); + } + } + let mut s = String::with_capacity(9); + for stack in stax { + s.push(stack.last().copied().unwrap()); + } + s }