mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Now with fewer branches
This commit is contained in:
@@ -39,17 +39,15 @@ pub fn part2(input: &[u8]) -> Result<String> {
|
|||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
|
|
||||||
for line in input.split(|&c| c == b'\n') {
|
for line in input.split(|&c| c == b'\n') {
|
||||||
let mut first = None;
|
let mut it = parser.find_overlapping_iter(line);
|
||||||
let mut last = 0;
|
if let Some(first) = it.next() {
|
||||||
|
let first = convert_id(first.pattern().as_u32())?;
|
||||||
|
let last = if let Some(last) = it.last() {
|
||||||
|
convert_id(last.pattern().as_u32())?
|
||||||
|
} else {
|
||||||
|
first
|
||||||
|
};
|
||||||
|
|
||||||
// Cannot use find_iter because it doesn't find overlapping matches.
|
|
||||||
for needle in parser.find_overlapping_iter(line) {
|
|
||||||
let digit = convert_id(needle.pattern().as_u32())?;
|
|
||||||
first.get_or_insert(digit);
|
|
||||||
last = digit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(first) = first {
|
|
||||||
sum += 10 * first + last;
|
sum += 10 * first + last;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user