diff --git a/2020/src/day06.rs b/2020/src/day06.rs index 19d5b41..06ae2a8 100644 --- a/2020/src/day06.rs +++ b/2020/src/day06.rs @@ -10,23 +10,18 @@ fn count(answers: Answers) -> u32 { } fn compute_answers(group: &str) -> Answers { - let mut questions = 0; - - for question in group.chars().filter(char::is_ascii_lowercase) { - questions |= 1 << (question as usize - 'a' as usize); - } - - questions + group + .chars() + .filter(char::is_ascii_lowercase) + .map(|q| 1 << (q as usize - 'a' as usize)) + .fold(0, |a, b| a | b) } fn count_answers_all(group: &str) -> u32 { - let mut combined = 0xffff_ffff; - - for line in group.split('\n') { - let single = compute_answers(line); - - combined &= single; - } + let combined = group + .split('\n') + .map(compute_answers) + .fold(0xffff_ffff, |a, b| a & b); count(combined) } @@ -38,7 +33,7 @@ impl Solution for Day06 { fn part1(&mut self, input: &mut dyn Read) -> String { let input: String = read_single_input(input); - let total: u32 = input.split("\n\n").map(compute_answers).map(count).sum(); + let total: u32 = input.split("\n\n").map(|g| count(compute_answers(g))).sum(); total.to_string() }