From 2e0eab1c03bef2bf34e6ecd6798db96184c9d522 Mon Sep 17 00:00:00 2001 From: Bauke Date: Mon, 3 Oct 2022 13:02:39 +0200 Subject: [PATCH] Use .retain(). --- source/year_2021/day_03/mod.rs | 12 ++++-------- source/year_2021/day_04/bingo.rs | 6 +----- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/source/year_2021/day_03/mod.rs b/source/year_2021/day_03/mod.rs index 67018cc..d55d7aa 100644 --- a/source/year_2021/day_03/mod.rs +++ b/source/year_2021/day_03/mod.rs @@ -64,10 +64,8 @@ fn part_2(input: &str) -> Result { .get(index) .ok_or_else(|| eyre!("Could not find most common bit"))?; let most_common = if bit >= &0 { '1' } else { '0' }; - most_common_lines = most_common_lines - .into_iter() - .filter(|line| line.chars().nth(*index) == Some(most_common)) - .collect(); + most_common_lines + .retain(|line| line.chars().nth(*index) == Some(most_common)); most_common_bits = count_bits(&most_common_lines.join("\n"))?; } @@ -76,10 +74,8 @@ fn part_2(input: &str) -> Result { .get(index) .ok_or_else(|| eyre!("Could not find least common bit"))?; let least_common = if bit < &0 { '1' } else { '0' }; - least_common_lines = least_common_lines - .into_iter() - .filter(|line| line.chars().nth(*index) == Some(least_common)) - .collect(); + least_common_lines + .retain(|line| line.chars().nth(*index) == Some(least_common)); least_common_bits = count_bits(&least_common_lines.join("\n"))?; } } diff --git a/source/year_2021/day_04/bingo.rs b/source/year_2021/day_04/bingo.rs index 03fddd2..6238b30 100644 --- a/source/year_2021/day_04/bingo.rs +++ b/source/year_2021/day_04/bingo.rs @@ -43,11 +43,7 @@ impl Bingo { return (self.boards.first().unwrap().clone(), number); } - self.boards = self - .boards - .into_iter() - .filter(|board| !board.has_won) - .collect(); + self.boards.retain(|board| !board.has_won); } unreachable!()