mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Rewrite using iterators
This improves codegen for part 1 considerably.
This commit is contained in:
@@ -10,23 +10,18 @@ fn count(answers: Answers) -> u32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn compute_answers(group: &str) -> Answers {
|
fn compute_answers(group: &str) -> Answers {
|
||||||
let mut questions = 0;
|
group
|
||||||
|
.chars()
|
||||||
for question in group.chars().filter(char::is_ascii_lowercase) {
|
.filter(char::is_ascii_lowercase)
|
||||||
questions |= 1 << (question as usize - 'a' as usize);
|
.map(|q| 1 << (q as usize - 'a' as usize))
|
||||||
}
|
.fold(0, |a, b| a | b)
|
||||||
|
|
||||||
questions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn count_answers_all(group: &str) -> u32 {
|
fn count_answers_all(group: &str) -> u32 {
|
||||||
let mut combined = 0xffff_ffff;
|
let combined = group
|
||||||
|
.split('\n')
|
||||||
for line in group.split('\n') {
|
.map(compute_answers)
|
||||||
let single = compute_answers(line);
|
.fold(0xffff_ffff, |a, b| a & b);
|
||||||
|
|
||||||
combined &= single;
|
|
||||||
}
|
|
||||||
|
|
||||||
count(combined)
|
count(combined)
|
||||||
}
|
}
|
||||||
@@ -38,7 +33,7 @@ impl Solution for Day06 {
|
|||||||
fn part1(&mut self, input: &mut dyn Read) -> String {
|
fn part1(&mut self, input: &mut dyn Read) -> String {
|
||||||
let input: String = read_single_input(input);
|
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()
|
total.to_string()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user