mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Slightly more natural part 1
This commit is contained in:
@@ -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,14 +8,17 @@ 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 {
|
||||||
sum += 10 * first + last;
|
sum += 10 * first + last;
|
||||||
|
|||||||
Reference in New Issue
Block a user