Rewrite using iterators

This improves codegen for part 1 considerably.
This commit is contained in:
2020-12-06 09:26:17 +01:00
parent ecade93f0f
commit 0bf021045c

View File

@@ -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()
}