Compare commits

..

3 commits

Author SHA1 Message Date
Antoine Martin 2c0bf85249 2021: day03: use lambda for part 2 2021-12-03 15:44:11 +01:00
Antoine Martin 1968c7894f 2021: day03: bench 2021-12-03 15:37:58 +01:00
Antoine Martin 7fb284bfe3 2021: day02: bench 2021-12-03 15:36:54 +01:00
2 changed files with 24 additions and 21 deletions

View file

@ -1,9 +1,13 @@
use criterion::{criterion_group, criterion_main, Criterion}; use criterion::{criterion_group, criterion_main, Criterion};
use aoc2021::day01; use aoc2021::day01;
use aoc2021::day02;
use aoc2021::day03;
fn aoc2021_all(c: &mut Criterion) { fn aoc2021_all(c: &mut Criterion) {
c.bench_function("day01", |b| b.iter(|| day01::run().unwrap())); c.bench_function("day01", |b| b.iter(|| day01::run().unwrap()));
c.bench_function("day02", |b| b.iter(|| day02::run().unwrap()));
c.bench_function("day03", |b| b.iter(|| day03::run().unwrap()));
} }
criterion_group! { criterion_group! {

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)]