From 3b737d4cef633f697a603f1b20c4bb96940060f3 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 5 Dec 2023 14:18:00 +0100 Subject: [PATCH] Refactor not to iterate over the temporary vector. --- src/day4/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) 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