diff --git a/src/day4/mod.rs b/src/day4/mod.rs index 388644d..c637b73 100644 --- a/src/day4/mod.rs +++ b/src/day4/mod.rs @@ -49,17 +49,16 @@ pub fn two() -> Result { let mut vec = Vec::::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] + })) } \ No newline at end of file