Optimized day 5 part 2
This commit is contained in:
parent
64c566cc6f
commit
ad8184a3d2
37
src/main.rs
37
src/main.rs
|
@ -58,29 +58,40 @@ fn parse(input: &[u8]) -> Stack {
|
||||||
let mut instructions = vec![];
|
let mut instructions = vec![];
|
||||||
|
|
||||||
while let Some(l) = line {
|
while let Some(l) = line {
|
||||||
let l: Vec<u8> = l
|
let mut output = vec![];
|
||||||
|
for &c in l
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&c| (b'0'..=b'9').contains(c) || c == &b' ')
|
.filter(|&c| (b'0'..=b'9').contains(c) || c == &b' ')
|
||||||
.cloned()
|
{
|
||||||
.collect();
|
if c == b' ' && output.last().map_or(true, |&c| c == b' ') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
output.push(c);
|
||||||
|
}
|
||||||
|
|
||||||
if l.is_empty() {
|
if output.is_empty() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
let numbers: Vec<u8> = l
|
let mut input = output.splitn(2, |&c| c == b' ');
|
||||||
.split(|&c| c == b' ')
|
let e1 = input.next().map_or(0, |c| {
|
||||||
.filter(|c| !c.is_empty())
|
|
||||||
.map(|c| {
|
|
||||||
c.get(1)
|
c.get(1)
|
||||||
.map_or(c[0] - b'0', |a| (c[0] - b'0') * 10 + a - b'0')
|
.map_or(c[0] - b'0', |a| (c[0] - b'0') * 10 + a - b'0')
|
||||||
})
|
});
|
||||||
.collect();
|
let mut input = input.next().unwrap().splitn(2, |&c| c == b' ');
|
||||||
|
let e2 = input.next().map_or(0, |c| {
|
||||||
|
c.get(1)
|
||||||
|
.map_or(c[0] - b'0', |a| (c[0] - b'0') * 10 + a - b'0')
|
||||||
|
});
|
||||||
|
let e3 = input.next().map_or(0, |c| {
|
||||||
|
c.get(1)
|
||||||
|
.map_or(c[0] - b'0', |a| (c[0] - b'0') * 10 + a - b'0')
|
||||||
|
});
|
||||||
|
|
||||||
instructions.push(Instruction {
|
instructions.push(Instruction {
|
||||||
count: numbers[0] as usize,
|
count: e1 as usize,
|
||||||
from: numbers[1] as usize,
|
from: e2 as usize,
|
||||||
to: numbers[2] as usize,
|
to: e3 as usize,
|
||||||
});
|
});
|
||||||
|
|
||||||
if let Some(r) = rest {
|
if let Some(r) = rest {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user