Now with more iterators and 10% perf gain

This commit is contained in:
2023-12-01 12:40:15 +01:00
parent 1541b82c11
commit 524527dbd1

View File

@@ -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())