1
Fork 0

Use .retain().

This commit is contained in:
Bauke 2022-10-03 13:02:39 +02:00
parent 639fe1a3b8
commit 2e0eab1c03
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 5 additions and 13 deletions

View File

@ -64,10 +64,8 @@ fn part_2(input: &str) -> Result<String> {
.get(index) .get(index)
.ok_or_else(|| eyre!("Could not find most common bit"))?; .ok_or_else(|| eyre!("Could not find most common bit"))?;
let most_common = if bit >= &0 { '1' } else { '0' }; let most_common = if bit >= &0 { '1' } else { '0' };
most_common_lines = most_common_lines most_common_lines
.into_iter() .retain(|line| line.chars().nth(*index) == Some(most_common));
.filter(|line| line.chars().nth(*index) == Some(most_common))
.collect();
most_common_bits = count_bits(&most_common_lines.join("\n"))?; most_common_bits = count_bits(&most_common_lines.join("\n"))?;
} }
@ -76,10 +74,8 @@ fn part_2(input: &str) -> Result<String> {
.get(index) .get(index)
.ok_or_else(|| eyre!("Could not find least common bit"))?; .ok_or_else(|| eyre!("Could not find least common bit"))?;
let least_common = if bit < &0 { '1' } else { '0' }; let least_common = if bit < &0 { '1' } else { '0' };
least_common_lines = least_common_lines least_common_lines
.into_iter() .retain(|line| line.chars().nth(*index) == Some(least_common));
.filter(|line| line.chars().nth(*index) == Some(least_common))
.collect();
least_common_bits = count_bits(&least_common_lines.join("\n"))?; least_common_bits = count_bits(&least_common_lines.join("\n"))?;
} }
} }

View File

@ -43,11 +43,7 @@ impl Bingo {
return (self.boards.first().unwrap().clone(), number); return (self.boards.first().unwrap().clone(), number);
} }
self.boards = self self.boards.retain(|board| !board.has_won);
.boards
.into_iter()
.filter(|board| !board.has_won)
.collect();
} }
unreachable!() unreachable!()