From a5adc0185e6cbd8e1de997615104de027f8a4644 Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Sat, 4 Dec 2021 15:45:57 +0100 Subject: [PATCH] 2021: day03: use Vec::retain ~30% improvement in benchmark! --- aoc2021/src/day03.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/aoc2021/src/day03.rs b/aoc2021/src/day03.rs index fababff..919e327 100644 --- a/aoc2021/src/day03.rs +++ b/aoc2021/src/day03.rs @@ -93,15 +93,7 @@ where let one_is_more_common = count_ones(&numbers, pos) >= ((numbers.len() + 1) / 2); let digit_of_interest = strat(one_is_more_common); - // TODO: use drain_filter when stable - let mut i = 0; - while i < numbers.len() { - if numbers[i].chars().nth(pos).unwrap() != digit_of_interest { - numbers.remove(i); - } else { - i += 1; - } - } + numbers.retain(|number| number.chars().nth(pos).unwrap() == digit_of_interest); } debug_assert_eq!(numbers.len(), 1);