aboutsummaryrefslogtreecommitdiff
path: root/2022/19/common.rs
diff options
context:
space:
mode:
Diffstat (limited to '2022/19/common.rs')
-rw-r--r--2022/19/common.rs20
1 files changed, 13 insertions, 7 deletions
diff --git a/2022/19/common.rs b/2022/19/common.rs
index 95b9a2d..c4177ac 100644
--- a/2022/19/common.rs
+++ b/2022/19/common.rs
@@ -63,7 +63,7 @@ pub struct Blueprint {
63 ore_bot_cost: Stock, 63 ore_bot_cost: Stock,
64 cla_bot_cost: Stock, 64 cla_bot_cost: Stock,
65 obs_bot_cost: Stock, 65 obs_bot_cost: Stock,
66 geo_bot_cost: Stock 66 geo_bot_cost: Stock,
67} 67}
68 68
69impl Blueprint { 69impl Blueprint {
@@ -118,8 +118,8 @@ pub fn most_geodes(
118 bp: &Blueprint, 118 bp: &Blueprint,
119 s: &Status, 119 s: &Status,
120 m: i32, 120 m: i32,
121 mem: &mut HashMap<(Status, i32), i32> 121 mem: &mut HashMap<(Status, i32), i64>
122) -> i32 { 122) -> i64 {
123 if m <= 1 { return 0; } 123 if m <= 1 { return 0; }
124 124
125 if let Some(r) = mem.get(&(s.clone(), m)) { return *r; } 125 if let Some(r) = mem.get(&(s.clone(), m)) { return *r; }
@@ -130,16 +130,22 @@ pub fn most_geodes(
130 // If a geode bot can be built, it is always the best thing to do 130 // If a geode bot can be built, it is always the best thing to do
131 if m > 1 && s.resources >= bp.geo_bot_cost { 131 if m > 1 && s.resources >= bp.geo_bot_cost {
132 new_status.resources -= &bp.geo_bot_cost; 132 new_status.resources -= &bp.geo_bot_cost;
133 let result = m - 1 + most_geodes(bp, &new_status, m-1, mem); 133 let result = (m - 1) as i64 + most_geodes(bp, &new_status, m-1, mem);
134 mem.insert((s.clone(), m), result); 134 mem.insert((s.clone(), m), result);
135 return result; 135 return result;
136 } 136 }
137 137
138 let mut result = 0; 138 let mut result = 0;
139 139
140 let can_obs = s.resources >= bp.obs_bot_cost; 140 // The second check is to avoid producing too many bots for each given
141 let can_cla = s.resources >= bp.cla_bot_cost; 141 // type of resource. For example, 5 ore bots will produce more ore
142 let can_ore = s.resources >= bp.ore_bot_cost; 142 // than the factory can ever consume, since all bots cost at most 4 ore.
143 let can_obs = s.resources >= bp.obs_bot_cost &&
144 s.bots.obs < bp.geo_bot_cost.obs;
145 let can_cla = s.resources >= bp.cla_bot_cost &&
146 s.bots.cla < bp.obs_bot_cost.cla;
147 let can_ore = s.resources >= bp.ore_bot_cost &&
148 s.bots.ore < 4;
143 149
144 if m > 2 && can_obs { 150 if m > 2 && can_obs {
145 new_status.resources -= &bp.obs_bot_cost; 151 new_status.resources -= &bp.obs_bot_cost;

Generated with cgit - Back to sebastiano.tronto.net