Refactor not to iterate over the temporary vector.
This commit is contained in:
parent
52db8b9109
commit
3b737d4cef
@ -49,17 +49,16 @@ pub fn two() -> Result<u32, AdventError> {
|
|||||||
let mut vec = Vec::<u32>::new();
|
let mut vec = Vec::<u32>::new();
|
||||||
vec.resize(games.len(), 1);
|
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 my_winning_numbers = game.my_numbers.intersection(&game.winning_numbers);
|
||||||
let nr_free_tickets = my_winning_numbers.count() as i32;
|
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 ) {
|
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 {
|
for _ in 0..nr_to_count {
|
||||||
vec[index as usize] += 1;
|
vec[index as usize] += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
acc + vec[vec_index]
|
||||||
|
}))
|
||||||
Ok(vec.iter().sum())
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user