From 950d82569ddf137f1674095bfbe6bfff8fce663f Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sun, 6 Jul 2025 00:18:18 +0200 Subject: Minor improvement using ref instead of copy --- 2022/19/a.rs | 2 +- 2022/19/common.rs | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to '2022/19') diff --git a/2022/19/a.rs b/2022/19/a.rs index 57e83e6..898debd 100644 --- a/2022/19/a.rs +++ b/2022/19/a.rs @@ -8,7 +8,7 @@ fn main() { let mut i = 1; let mut sum = 0; for bp in read_blueprints_from_stdin() { - let mg = most_geodes(&bp, Status::new(), MINUTES, &mut mem); + let mg = most_geodes(&bp, &Status::new(), MINUTES, &mut mem); println!("{i}: {mg}"); sum += i * mg; mem.clear(); diff --git a/2022/19/common.rs b/2022/19/common.rs index 060d4fc..95b9a2d 100644 --- a/2022/19/common.rs +++ b/2022/19/common.rs @@ -116,22 +116,22 @@ pub fn read_blueprints_from_stdin() -> Vec { pub fn most_geodes( bp: &Blueprint, - s: Status, + s: &Status, m: i32, mem: &mut HashMap<(Status, i32), i32> ) -> i32 { if m <= 1 { return 0; } - if let Some(r) = mem.get(&(s, m)) { return *r; } + if let Some(r) = mem.get(&(s.clone(), m)) { return *r; } - let mut new_status = s; + let mut new_status = s.clone(); new_status.resources += &new_status.bots; // If a geode bot can be built, it is always the best thing to do if m > 1 && s.resources >= bp.geo_bot_cost { new_status.resources -= &bp.geo_bot_cost; - let result = m - 1 + most_geodes(bp, new_status, m-1, mem); - mem.insert((s, m), result); + let result = m - 1 + most_geodes(bp, &new_status, m-1, mem); + mem.insert((s.clone(), m), result); return result; } @@ -144,7 +144,7 @@ pub fn most_geodes( if m > 2 && can_obs { new_status.resources -= &bp.obs_bot_cost; new_status.bots.obs += 1; - result = max(result, most_geodes(bp, new_status, m-1, mem)); + result = max(result, most_geodes(bp, &new_status, m-1, mem)); new_status.bots.obs -= 1; new_status.resources += &bp.obs_bot_cost; } @@ -152,7 +152,7 @@ pub fn most_geodes( if m > 3 && can_cla { new_status.resources -= &bp.cla_bot_cost; new_status.bots.cla += 1; - result = max(result, most_geodes(bp, new_status, m-1, mem)); + result = max(result, most_geodes(bp, &new_status, m-1, mem)); new_status.bots.cla -= 1; new_status.resources += &bp.cla_bot_cost; } @@ -160,15 +160,15 @@ pub fn most_geodes( if m > 4 && can_ore { new_status.resources -= &bp.ore_bot_cost; new_status.bots.ore += 1; - result = max(result, most_geodes(bp, new_status, m-1, mem)); + result = max(result, most_geodes(bp, &new_status, m-1, mem)); new_status.bots.ore -= 1; new_status.resources += &bp.ore_bot_cost; } if !can_obs || !can_cla || !can_ore { - result = max(result, most_geodes(bp, new_status, m-1, mem)); + result = max(result, most_geodes(bp, &new_status, m-1, mem)); } - mem.insert((s, m), result); + mem.insert((s.clone(), m), result); result } -- cgit v1.3