From 96441ccdbd86453f6c67a0909f2dd2c06523b6c8 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Tue, 6 Dec 2022 14:55:16 -0800 Subject: [PATCH] clean up day5, don't use itertools --- 2022-aoc/Cargo.toml | 8 +++---- 2022-aoc/src/d5.rs | 57 ++++++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 38 deletions(-) diff --git a/2022-aoc/Cargo.toml b/2022-aoc/Cargo.toml index 64791f1..2199451 100644 --- a/2022-aoc/Cargo.toml +++ b/2022-aoc/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -aoc-runner = "0.3.0" -aoc-runner-derive = "0.3.0" -itertools = "0.10.5" -regex = "1.7.0" +aoc-runner = "0.3" +aoc-runner-derive = "0.3" +#itertools = "0.10.5" +regex = "1.7" diff --git a/2022-aoc/src/d5.rs b/2022-aoc/src/d5.rs index 10cf42f..98ef932 100644 --- a/2022-aoc/src/d5.rs +++ b/2022-aoc/src/d5.rs @@ -1,53 +1,43 @@ use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; -use itertools::Itertools; type Stax = Vec>; type Inst = (usize, usize, usize); -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; + let mut parsing_crates = true; 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()); - } + if parsing_crates { + if line.trim().starts_with('1') { + continue; + } + if line.trim().is_empty() { + parsing_crates = false; + } + for (i, cell) in line.as_bytes().chunks(4).enumerate() { + let cell = std::str::from_utf8(cell).unwrap().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, - )); - } + } else { + 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(); } @@ -85,9 +75,8 @@ fn p2((stax, inst): &(Stax, Vec)) -> String { let c = from.pop().unwrap(); tmp.push(c); } - for c in tmp.into_iter().rev() { - stax[*to].push(c); - } + tmp.reverse(); + stax[*to].append(&mut tmp); } let mut s = String::with_capacity(9); for stack in stax {