From 4ee6a8205629ea5d8f7bfbf30e6218446f9c5bdd Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 2 Dec 2023 14:11:18 +0100 Subject: [PATCH] Now with fewer branches --- 2023/src/day01.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/2023/src/day01.rs b/2023/src/day01.rs index d6025f8..599280a 100644 --- a/2023/src/day01.rs +++ b/2023/src/day01.rs @@ -39,17 +39,15 @@ pub fn part2(input: &[u8]) -> Result { let mut sum = 0; for line in input.split(|&c| c == b'\n') { - let mut first = None; - let mut last = 0; + let mut it = parser.find_overlapping_iter(line); + 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; } }