Use retain instead of cloning

This commit is contained in:
Tyler Hallada 2021-12-03 16:52:12 -05:00
parent 87fdd366ca
commit 18179dde06

View File

@ -52,29 +52,17 @@ fn solve_part2(input: &str) -> Result<i32> {
if oxygen.len() > 1 { if oxygen.len() > 1 {
if oxygen_most_common[i] as f32 >= (oxygen.len() as f32 / 2.0) { if oxygen_most_common[i] as f32 >= (oxygen.len() as f32 / 2.0) {
oxygen = oxygen oxygen.retain(|l| l.chars().nth(i).unwrap() == '1');
.into_iter()
.filter(|l| l.chars().nth(i).unwrap() == '1')
.collect();
} else { } else {
oxygen = oxygen oxygen.retain(|l| l.chars().nth(i).unwrap() == '0');
.into_iter()
.filter(|l| l.chars().nth(i).unwrap() == '0')
.collect();
} }
} }
if co2.len() > 1 { if co2.len() > 1 {
if co2_most_common[i] as f32 >= (co2.len() as f32 / 2.0) { if co2_most_common[i] as f32 >= (co2.len() as f32 / 2.0) {
co2 = co2 co2.retain(|l| l.chars().nth(i).unwrap() == '0');
.into_iter()
.filter(|l| l.chars().nth(i).unwrap() == '0')
.collect();
} else { } else {
co2 = co2 co2.retain(|l| l.chars().nth(i).unwrap() == '1');
.into_iter()
.filter(|l| l.chars().nth(i).unwrap() == '1')
.collect();
} }
} }