1
0
Fork 0

Much cleaner loop for p2

This commit is contained in:
Vivianne 2022-12-02 23:30:14 -08:00
parent 18915dfd8a
commit 7663397c1b

View file

@ -60,22 +60,16 @@ fn part2(lines: &[String]) {
let mut sum: u32 = 0; let mut sum: u32 = 0;
for chunk in lines.chunks(GROUP_SIZE) { for chunk in lines.chunks(GROUP_SIZE) {
let mut outlier: Option<char> = None; for i in 0..GROUP_SIZE - 1 {
'outlier_search: for i in 0..GROUP_SIZE { sets[i].clear();
if i != GROUP_SIZE - 1 { chunk[i].chars().collect_into(&mut sets[i]);
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;
}
}
}
} }
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(); let priority = to_priority(outlier).unwrap();
dbg!(outlier, priority); dbg!(outlier, priority);
sum += priority; sum += priority;