clean up day5, don't use itertools

This commit is contained in:
Joe Ardent 2022-12-06 14:55:16 -08:00
parent 5779749423
commit 96441ccdbd
2 changed files with 27 additions and 38 deletions

View file

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
aoc-runner = "0.3.0" aoc-runner = "0.3"
aoc-runner-derive = "0.3.0" aoc-runner-derive = "0.3"
itertools = "0.10.5" #itertools = "0.10.5"
regex = "1.7.0" regex = "1.7"

View file

@ -1,53 +1,43 @@
use aoc_runner_derive::{aoc as aoc_run, aoc_generator}; use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
use itertools::Itertools;
type Stax = Vec<Vec<char>>; type Stax = Vec<Vec<char>>;
type Inst = (usize, usize, usize); type Inst = (usize, usize, usize);
enum Pstate {
Setup,
Instr,
}
#[aoc_generator(day5)] #[aoc_generator(day5)]
fn parse_input_day1(input: &str) -> (Stax, Vec<Inst>) { fn parse_input_day1(input: &str) -> (Stax, Vec<Inst>) {
let mut out = Vec::new(); let mut out = Vec::new();
let mut inst = Vec::new(); let mut inst = Vec::new();
let mut state = Pstate::Setup; let mut parsing_crates = true;
for _ in 0..9 { for _ in 0..9 {
out.push(Vec::new()); out.push(Vec::new());
} }
let re = regex::Regex::new(r"^move (\d+) from (\d+) to (\d+)$").unwrap(); let re = regex::Regex::new(r"^move (\d+) from (\d+) to (\d+)$").unwrap();
for line in input.lines() { for line in input.lines() {
match state { if parsing_crates {
Pstate::Setup => { if line.trim().starts_with('1') {
if line.trim().starts_with('1') { continue;
continue; }
} if line.trim().is_empty() {
if line.trim().is_empty() { parsing_crates = false;
state = Pstate::Instr; }
} for (i, cell) in line.as_bytes().chunks(4).enumerate() {
for (i, cell) in line.chars().chunks(4).into_iter().enumerate() { let cell = std::str::from_utf8(cell).unwrap().trim();
let cell: String = cell.collect(); if !cell.is_empty() {
let cell = cell.trim(); let cell = cell.strip_prefix('[').unwrap().strip_suffix(']').unwrap();
if !cell.is_empty() { out[i].push(cell.chars().next().unwrap());
let cell = cell.strip_prefix('[').unwrap().strip_suffix(']').unwrap();
out[i].push(cell.chars().next().unwrap());
}
} }
} }
// parse the instructions } else {
Pstate::Instr => { 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::<usize>().unwrap() - 1,
ins[2].parse::<usize>().unwrap() - 1, ins[3].parse::<usize>().unwrap() - 1,
ins[3].parse::<usize>().unwrap() - 1, ));
));
}
} }
} }
for stack in out.iter_mut() { for stack in out.iter_mut() {
stack.reverse(); stack.reverse();
} }
@ -85,9 +75,8 @@ fn p2((stax, inst): &(Stax, Vec<Inst>)) -> String {
let c = from.pop().unwrap(); let c = from.pop().unwrap();
tmp.push(c); tmp.push(c);
} }
for c in tmp.into_iter().rev() { tmp.reverse();
stax[*to].push(c); 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 {