1
0

Optimized day 5 part 2

This commit is contained in:
Ishan Jain 2022-12-05 13:21:41 +05:30
parent e2e9d95a16
commit 64c566cc6f
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -1,14 +1,14 @@
#![feature(test)] #![feature(test)]
extern crate test; extern crate test;
const INPUTS: [&str; 2] = [ const INPUTS: [&[u8]; 2] = [
include_str!("../inputs/sample.txt"), include_bytes!("../inputs/sample.txt"),
include_str!("../inputs/input.txt"), include_bytes!("../inputs/input.txt"),
]; ];
#[derive(Debug)] #[derive(Debug)]
struct Stack { struct Stack {
stacks: Vec<Vec<char>>, stacks: Vec<Vec<u8>>,
instructions: Vec<Instruction>, instructions: Vec<Instruction>,
} }
@ -19,63 +19,79 @@ struct Instruction {
to: usize, to: usize,
} }
fn parse(input: &'static str) -> Stack { fn parse(input: &[u8]) -> Stack {
let mut v = input.split("\n\n"); let mut stacks = vec![];
let mut stack_input = v let mut input = input.splitn(2, |&c| c == b'\n');
.next() let mut line = input.next().unwrap();
.unwrap() let mut rest = input.next().unwrap();
.lines()
.map(|c| c.chars().collect::<Vec<char>>())
.rev();
let mut stacks: Vec<Vec<char>> = vec![];
let stack_names = stack_input.next().unwrap(); while rest[0] != b'\n' {
let mut i = 1;
let containers: Vec<Vec<char>> = stack_input.collect(); while (3 * (i - 1) + i) < line.len() {
if stacks.len() < i {
for (i, v) in stack_names.into_iter().enumerate() { stacks.push(vec![]);
if v == ' ' {
continue;
}
let v: usize = (v as u8 - b'0') as usize;
if stacks.len() <= v - 1 {
stacks.push(vec![]);
}
for container in containers.iter() {
if container[i] == ' ' {
continue;
} }
stacks[v - 1].push(container[i]);
if (b'A'..=b'Z').contains(&line[3 * (i - 1) + i]) {
stacks[i - 1].push(line[3 * (i - 1) + i]);
}
i += 1;
}
let mut temp = rest.splitn(2, |&c| c == b'\n');
line = temp.next().unwrap();
rest = temp.next().unwrap();
}
rest = &rest[1..];
let stacks: Vec<Vec<u8>> = stacks
.into_iter()
.map(|c| c.into_iter().rev().collect())
.collect();
let mut input = rest.splitn(2, |&c| c == b'\n');
let mut line = input.next();
let mut rest = input.next();
let mut instructions = vec![];
while let Some(l) = line {
let l: Vec<u8> = l
.iter()
.filter(|&c| (b'0'..=b'9').contains(c) || c == &b' ')
.cloned()
.collect();
if l.is_empty() {
break;
}
let numbers: Vec<u8> = l
.split(|&c| c == b' ')
.filter(|c| !c.is_empty())
.map(|c| {
c.get(1)
.map_or(c[0] - b'0', |a| (c[0] - b'0') * 10 + a - b'0')
})
.collect();
instructions.push(Instruction {
count: numbers[0] as usize,
from: numbers[1] as usize,
to: numbers[2] as usize,
});
if let Some(r) = rest {
let mut input = r.splitn(2, |&c| c == b'\n');
line = input.next();
rest = input.next();
} else {
break;
} }
} }
let instructions: Vec<Instruction> = v
.next()
.unwrap()
.lines()
.map(|line: &str| {
let line: String = line
.chars()
.filter(|c| c.is_numeric() | c.is_whitespace())
.collect();
let numbers: Vec<u8> = line
.split(' ')
.filter(|c| !c.is_empty())
.map(|c| c.parse::<u8>().unwrap())
.collect();
Instruction {
count: numbers[0] as usize,
from: numbers[1] as usize,
to: numbers[2] as usize,
}
})
.collect();
Stack { Stack {
instructions, instructions,
stacks, stacks,
@ -95,7 +111,7 @@ fn solution(mut input: Stack) -> String {
for ins in input.instructions { for ins in input.instructions {
let l = input.stacks[ins.from - 1].len(); let l = input.stacks[ins.from - 1].len();
let crates: Vec<char> = input.stacks[ins.from - 1] let crates: Vec<u8> = input.stacks[ins.from - 1]
.drain(l.saturating_sub(ins.count)..l) .drain(l.saturating_sub(ins.count)..l)
.collect(); .collect();
@ -109,6 +125,7 @@ fn solution(mut input: Stack) -> String {
.into_iter() .into_iter()
.filter(|c| !c.is_empty()) .filter(|c| !c.is_empty())
.map(|c| *c.last().unwrap()) .map(|c| *c.last().unwrap())
.map(|c| c as char)
.collect() .collect()
} }