From fc5c6218e5d4b032590e1ac0f8d28168d1d18bc5 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 11 Dec 2023 09:47:36 -0500 Subject: [PATCH] Optimize 2023 day4 solution --- 2023/day4/src/main.rs | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/2023/day4/src/main.rs b/2023/day4/src/main.rs index d9b5829..b610a7c 100644 --- a/2023/day4/src/main.rs +++ b/2023/day4/src/main.rs @@ -86,22 +86,20 @@ impl GameMap { None => 1, }; - for _ in 0..copy_count { - let add_range = id + 1..=id + match_count; - for n in add_range { - let raw_count = count_map.get(&n); - if let Some(count) = raw_count { - count_map.insert(n, *count + 1); - let copy_count = match copy_map.get(&n) { - Some(num) => *num + 1, - None => 1, - }; - copy_map.insert(n, copy_count); - } else { - // No value means the value of n is greater than - // the amount of original cards - break; - } + let add_range = id + 1..=id + match_count; + for n in add_range { + let raw_count = count_map.get(&n); + if let Some(count) = raw_count { + count_map.insert(n, *count + copy_count); + let new_copy_count = match copy_map.get(&n) { + Some(num) => *num + copy_count, + None => copy_count, + }; + copy_map.insert(n, new_copy_count); + } else { + // No value means the value of n is greater than + // the amount of original cards + break; } } }