From 7663397c1b6d81f5c2b373d4b640e2f1a8417b60 Mon Sep 17 00:00:00 2001 From: Vivianne Langdon Date: Fri, 2 Dec 2022 23:30:14 -0800 Subject: [PATCH] Much cleaner loop for p2 --- src/bin/p03.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/bin/p03.rs b/src/bin/p03.rs index c546fd5..641c693 100644 --- a/src/bin/p03.rs +++ b/src/bin/p03.rs @@ -60,22 +60,16 @@ fn part2(lines: &[String]) { let mut sum: u32 = 0; for chunk in lines.chunks(GROUP_SIZE) { - let mut outlier: Option = None; - 'outlier_search: for i in 0..GROUP_SIZE { - if i != GROUP_SIZE - 1 { - sets[i].clear(); - chunk[i].chars().collect_into(&mut sets[i]); - } else { - for c in chunk[i].chars() { - if sets[0..GROUP_SIZE - 1].into_iter().all(|s| s.contains(&c)) { - outlier = Some(c); - break 'outlier_search; - } - } - } + for i in 0..GROUP_SIZE - 1 { + sets[i].clear(); + chunk[i].chars().collect_into(&mut sets[i]); } - let outlier = outlier.expect("Expect at least one outlier"); + let outlier = chunk[GROUP_SIZE - 1] + .chars() + .find(|c| sets[0..GROUP_SIZE - 1].into_iter().all(|s| s.contains(&c))) + .expect("Expect at least one outlier"); + let priority = to_priority(outlier).unwrap(); dbg!(outlier, priority); sum += priority;