Implement faster day 6 part 2

This commit is contained in:
2020-12-06 11:27:12 -08:00
parent 99001d75b5
commit 4172592120
2 changed files with 62 additions and 31 deletions

View File

@@ -102,6 +102,33 @@ fn solution2(input: &str) -> usize {
.sum()
}
#[aoc(day6, part2, faster)]
fn solution2_faster(input: &str) -> usize {
input
.split("\n\n")
.filter_map(|group| {
group.split('\n').fold(None, |acc: Option<HashSet<_>>, p| {
match acc {
None => {
// Add all the letters for the first set.
Some(p.chars().collect())
}
Some(acc) => {
// Remove from acc any letters not in p.
let tmp = Some(
p.chars()
.filter(|c| acc.contains(c))
.collect::<HashSet<_>>(),
);
tmp
}
}
})
})
.map(|set| set.len())
.sum()
}
#[cfg(test)]
mod tests {
use super::*;