Optimize 2023 day4 solution

This commit is contained in:
Timothy Warren 2023-12-11 09:47:36 -05:00
parent d3f5e06228
commit fc5c6218e5

View File

@ -86,17 +86,16 @@ impl GameMap {
None => 1, None => 1,
}; };
for _ in 0..copy_count {
let add_range = id + 1..=id + match_count; let add_range = id + 1..=id + match_count;
for n in add_range { for n in add_range {
let raw_count = count_map.get(&n); let raw_count = count_map.get(&n);
if let Some(count) = raw_count { if let Some(count) = raw_count {
count_map.insert(n, *count + 1); count_map.insert(n, *count + copy_count);
let copy_count = match copy_map.get(&n) { let new_copy_count = match copy_map.get(&n) {
Some(num) => *num + 1, Some(num) => *num + copy_count,
None => 1, None => copy_count,
}; };
copy_map.insert(n, copy_count); copy_map.insert(n, new_copy_count);
} else { } else {
// No value means the value of n is greater than // No value means the value of n is greater than
// the amount of original cards // the amount of original cards
@ -104,7 +103,6 @@ impl GameMap {
} }
} }
} }
}
count_map count_map
} }