mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Now with more iterators and 10% perf gain
This commit is contained in:
@@ -5,27 +5,18 @@ pub fn part1(input: &[u8]) -> Result<String> {
|
|||||||
let mut it = input.iter();
|
let mut it = input.iter();
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
|
|
||||||
loop {
|
while let Some(&first) = it.find(|s| s.is_ascii_digit()) {
|
||||||
let mut first = None;
|
let mut last = first;
|
||||||
let mut last = 0;
|
|
||||||
|
|
||||||
for &c in &mut it {
|
for &c in &mut it {
|
||||||
match c {
|
match c {
|
||||||
d @ b'0'..=b'9' => {
|
d @ b'0'..=b'9' => last = d,
|
||||||
let digit = u32::from(d - b'0');
|
|
||||||
first.get_or_insert(digit);
|
|
||||||
last = digit;
|
|
||||||
}
|
|
||||||
b'\n' => break,
|
b'\n' => break,
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(first) = first {
|
sum += u32::from(10 * (first - b'0') + last - b'0');
|
||||||
sum += 10 * first + last;
|
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(sum.to_string())
|
Ok(sum.to_string())
|
||||||
|
|||||||
Reference in New Issue
Block a user