done with part 2

This commit is contained in:
Joe Ardent 2022-12-05 16:46:17 -08:00
parent e7c4253f1c
commit 5779749423
1 changed files with 25 additions and 10 deletions

View File

@ -2,7 +2,7 @@ use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
use itertools::Itertools; use itertools::Itertools;
type Stax = Vec<Vec<char>>; type Stax = Vec<Vec<char>>;
type Inst = (u8, u8, u8); type Inst = (usize, usize, usize);
enum Pstate { enum Pstate {
Setup, Setup,
@ -42,8 +42,8 @@ fn parse_input_day1(input: &str) -> (Stax, Vec<Inst>) {
let ins = re.captures(line).unwrap(); let ins = re.captures(line).unwrap();
inst.push(( inst.push((
ins[1].parse().unwrap(), ins[1].parse().unwrap(),
ins[2].parse::<u8>().unwrap() - 1, ins[2].parse::<usize>().unwrap() - 1,
ins[3].parse::<u8>().unwrap() - 1, ins[3].parse::<usize>().unwrap() - 1,
)); ));
} }
} }
@ -59,14 +59,13 @@ fn part1((stax, inst): &(Stax, Vec<Inst>)) -> String {
let mut stax = stax.to_owned(); let mut stax = stax.to_owned();
for ins in inst.iter() { for ins in inst.iter() {
let (amt, from, to) = ins; let (amt, from, to) = ins;
let mut from = &mut stax[*from as usize]; let from = &mut stax[*from];
let mut tmp = Vec::with_capacity(*amt as usize); let mut tmp = Vec::with_capacity(*amt);
for _ in 0..*amt { for _ in 0..*amt {
let from = &mut from;
let t = from.pop().unwrap(); let t = from.pop().unwrap();
tmp.push(t); tmp.push(t);
} }
stax[*to as usize].append(&mut tmp); stax[*to].append(&mut tmp);
} }
let mut s = String::with_capacity(9); let mut s = String::with_capacity(9);
for stack in stax { for stack in stax {
@ -76,7 +75,23 @@ fn part1((stax, inst): &(Stax, Vec<Inst>)) -> String {
} }
#[aoc_run(day5, part2)] #[aoc_run(day5, part2)]
fn p2((stax, inst): &(Stax, Vec<Inst>)) -> u32 { fn p2((stax, inst): &(Stax, Vec<Inst>)) -> String {
// let mut stax = stax.to_owned();
0 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
} }