Slightly more natural part 1

This commit is contained in:
2023-12-01 10:22:26 +01:00
parent 98559e2010
commit 28178c8a73

View File

@@ -1,12 +1,5 @@
use anyhow::Result; use anyhow::Result;
fn to_digit(c: u8) -> Option<u32> {
match c {
b'0'..=b'9' => Some(u32::from(c - b'0')),
_ => None,
}
}
pub fn part1(input: &[u8]) -> Result<String> { 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;
@@ -15,13 +8,16 @@ pub fn part1(input: &[u8]) -> Result<String> {
let mut first = None; let mut first = None;
let mut last = 0; let mut last = 0;
for digit in it for &c in &mut it {
.by_ref() match c {
.take_while(|&&c| c != b'\n') d @ b'0'..=b'9' => {
.filter_map(|&c| to_digit(c)) let digit = u32::from(d - b'0');
{ first.get_or_insert(digit);
first.get_or_insert(digit); last = digit;
last = digit; }
b'\n' => break,
_ => continue,
}
} }
if let Some(first) = first { if let Some(first) = first {