From e7c4253f1c11263d438f1b1a36fa1f62fd2bfee2 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Mon, 5 Dec 2022 15:33:13 -0800 Subject: [PATCH] day5, part1 done --- 2022-aoc/Cargo.toml | 2 ++ 2022-aoc/src/d5.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++ 2022-aoc/src/lib.rs | 1 + 3 files changed, 85 insertions(+) create mode 100644 2022-aoc/src/d5.rs diff --git a/2022-aoc/Cargo.toml b/2022-aoc/Cargo.toml index 03aa5e0..64791f1 100644 --- a/2022-aoc/Cargo.toml +++ b/2022-aoc/Cargo.toml @@ -8,3 +8,5 @@ edition = "2021" [dependencies] aoc-runner = "0.3.0" aoc-runner-derive = "0.3.0" +itertools = "0.10.5" +regex = "1.7.0" diff --git a/2022-aoc/src/d5.rs b/2022-aoc/src/d5.rs new file mode 100644 index 0000000..3875b71 --- /dev/null +++ b/2022-aoc/src/d5.rs @@ -0,0 +1,82 @@ +use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; +use itertools::Itertools; + +type Stax = Vec>; +type Inst = (u8, u8, u8); + +enum Pstate { + Setup, + Instr, +} + +#[aoc_generator(day5)] +fn parse_input_day1(input: &str) -> (Stax, Vec) { + let mut out = Vec::new(); + let mut inst = Vec::new(); + let mut state = Pstate::Setup; + for _ in 0..9 { + out.push(Vec::new()); + } + + let re = regex::Regex::new(r"^move (\d+) from (\d+) to (\d+)$").unwrap(); + for line in input.lines() { + match state { + Pstate::Setup => { + if line.trim().starts_with('1') { + continue; + } + if line.trim().is_empty() { + state = Pstate::Instr; + } + for (i, cell) in line.chars().chunks(4).into_iter().enumerate() { + let cell: String = cell.collect(); + let cell = cell.trim(); + if !cell.is_empty() { + let cell = cell.strip_prefix('[').unwrap().strip_suffix(']').unwrap(); + out[i].push(cell.chars().next().unwrap()); + } + } + } + // parse the instructions + Pstate::Instr => { + let ins = re.captures(line).unwrap(); + inst.push(( + ins[1].parse().unwrap(), + ins[2].parse::().unwrap() - 1, + ins[3].parse::().unwrap() - 1, + )); + } + } + } + for stack in out.iter_mut() { + stack.reverse(); + } + (out, inst) +} + +#[aoc_run(day5, part1)] +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); + for _ in 0..*amt { + let from = &mut from; + let t = from.pop().unwrap(); + tmp.push(t); + } + stax[*to as usize].append(&mut tmp); + } + let mut s = String::with_capacity(9); + for stack in stax { + s.push(stack.last().copied().unwrap()); + } + s +} + +#[aoc_run(day5, part2)] +fn p2((stax, inst): &(Stax, Vec)) -> u32 { + // + 0 +} diff --git a/2022-aoc/src/lib.rs b/2022-aoc/src/lib.rs index 435698c..0457abf 100644 --- a/2022-aoc/src/lib.rs +++ b/2022-aoc/src/lib.rs @@ -4,5 +4,6 @@ mod d1; mod d2; mod d3; mod d4; +mod d5; aoc_lib! { year = 2022 }