Refactor not to iterate over the temporary vector.

This commit is contained in:
Dennis Brentjes 2023-12-05 14:18:00 +01:00
parent 52db8b9109
commit 3b737d4cef

View File

@ -49,17 +49,16 @@ pub fn two() -> Result<u32, AdventError> {
let mut vec = Vec::<u32>::new();
vec.resize(games.len(), 1);
for game in games.into_iter() {
Ok(games.into_iter().fold(0, | acc, game | {
let my_winning_numbers = game.my_numbers.intersection(&game.winning_numbers);
let nr_free_tickets = my_winning_numbers.count() as i32;
let vec_index: usize = (game.id - 1) as usize;
for index in game.id..(game.id + nr_free_tickets ) {
let nr_to_count = vec[(game.id - 1) as usize];
let nr_to_count = vec[vec_index];
for _ in 0..nr_to_count {
vec[index as usize] += 1;
}
}
}
Ok(vec.iter().sum())
acc + vec[vec_index]
}))
}