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;
for chunk in lines.chunks(GROUP_SIZE) {
let mut outlier: Option<char> = 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;