2021: day03: use lambda for part 2

This commit is contained in:
Antoine Martin 2021-12-03 15:44:11 +01:00
parent 1968c7894f
commit 2c0bf85249

View file

@ -73,12 +73,10 @@ fn part2(input: &str) -> Result<u64> {
Ok(oxygen_generator_rating * co2_scrubber_rating) Ok(oxygen_generator_rating * co2_scrubber_rating)
} }
enum FilterStrategy { fn filter_by_strat<Strat>(binary_numbers: &[&str], size: usize, strat: Strat) -> Result<u64>
MostCommon, where
LeastCommon, Strat: Fn(bool) -> char,
} {
fn filter_by_strat(binary_numbers: &[&str], size: usize, strat: FilterStrategy) -> Result<u64> {
let mut numbers = binary_numbers.to_vec(); let mut numbers = binary_numbers.to_vec();
for pos in 0..size { for pos in 0..size {
@ -87,19 +85,8 @@ fn filter_by_strat(binary_numbers: &[&str], size: usize, strat: FilterStrategy)
break; break;
} }
let digit_of_interest = if count_ones(&numbers, pos) >= ((numbers.len() + 1) / 2) { let one_is_more_common = count_ones(&numbers, pos) >= ((numbers.len() + 1) / 2);
// majority of ones, or equality let digit_of_interest = strat(one_is_more_common);
match strat {
FilterStrategy::MostCommon => '1',
FilterStrategy::LeastCommon => '0',
}
} else {
// majority of zeroes
match strat {
FilterStrategy::MostCommon => '0',
FilterStrategy::LeastCommon => '1',
}
};
// TODO: use drain_filter when stable // TODO: use drain_filter when stable
let mut i = 0; let mut i = 0;
@ -121,14 +108,26 @@ fn filter_by_strat(binary_numbers: &[&str], size: usize, strat: FilterStrategy)
/// position, and keep only numbers with that bit in that position. If 0 and 1 are equally common, /// position, and keep only numbers with that bit in that position. If 0 and 1 are equally common,
/// keep values with a 1 in the position being considered. /// keep values with a 1 in the position being considered.
fn compute_oxygen_generator_rating(binary_numbers: &[&str], size: usize) -> Result<u64> { fn compute_oxygen_generator_rating(binary_numbers: &[&str], size: usize) -> Result<u64> {
filter_by_strat(binary_numbers, size, FilterStrategy::MostCommon) filter_by_strat(binary_numbers, size, |one_is_more_common| {
if one_is_more_common {
'1'
} else {
'0'
}
})
} }
/// To find CO2 scrubber rating, determine the least common value (0 or 1) in the current bit /// To find CO2 scrubber rating, determine the least common value (0 or 1) in the current bit
/// position, and keep only numbers with that bit in that position. If 0 and 1 are equally /// position, and keep only numbers with that bit in that position. If 0 and 1 are equally
/// common, keep values with a 0 in the position being considered. /// common, keep values with a 0 in the position being considered.
fn compute_co2_scrubber_rating(binary_numbers: &[&str], size: usize) -> Result<u64> { fn compute_co2_scrubber_rating(binary_numbers: &[&str], size: usize) -> Result<u64> {
filter_by_strat(binary_numbers, size, FilterStrategy::LeastCommon) filter_by_strat(binary_numbers, size, |one_is_more_common| {
if one_is_more_common {
'0'
} else {
'1'
}
})
} }
#[cfg(test)] #[cfg(test)]