aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2022/19/a.rs2
-rw-r--r--2022/19/common.rs18
2 files changed, 7 insertions, 13 deletions
diff --git a/2022/19/a.rs b/2022/19/a.rs
index 7264579..57e83e6 100644
--- a/2022/19/a.rs
+++ b/2022/19/a.rs
@@ -8,7 +8,7 @@ fn main() {
8 let mut i = 1; 8 let mut i = 1;
9 let mut sum = 0; 9 let mut sum = 0;
10 for bp in read_blueprints_from_stdin() { 10 for bp in read_blueprints_from_stdin() {
11 let mg = most_geodes(&bp, Status::new(), MINUTES, &mut mem, 0); 11 let mg = most_geodes(&bp, Status::new(), MINUTES, &mut mem);
12 println!("{i}: {mg}"); 12 println!("{i}: {mg}");
13 sum += i * mg; 13 sum += i * mg;
14 mem.clear(); 14 mem.clear();
diff --git a/2022/19/common.rs b/2022/19/common.rs
index cc11460..319363e 100644
--- a/2022/19/common.rs
+++ b/2022/19/common.rs
@@ -122,17 +122,11 @@ pub fn most_geodes(
122 bp: &Blueprint, 122 bp: &Blueprint,
123 s: Status, 123 s: Status,
124 m: i32, 124 m: i32,
125 mem: &mut HashMap<(Status, i32), i32>, 125 mem: &mut HashMap<(Status, i32), i32>
126 lower_bound: i32
127) -> i32 { 126) -> i32 {
128 if m == 0 { return s.resources.geo; } 127 if m == 0 { return s.resources.geo; }
129 if m == 1 { return s.resources.geo + s.bots.geo; } 128 if m == 1 { return s.resources.geo + s.bots.geo; }
130 129
131 // Estimate how much we can get at most, assuming one geode bot
132 // can be built each turn from now on
133 let upper_bound = s.resources.geo + s.bots.geo + (m-1)*(m-2)/2;
134 if upper_bound <= lower_bound { return 0; }
135
136 if let Some(r) = mem.get(&(s, m)) { return *r; } 130 if let Some(r) = mem.get(&(s, m)) { return *r; }
137 131
138 let mut new_status = s; 132 let mut new_status = s;
@@ -143,7 +137,7 @@ pub fn most_geodes(
143 if m > 1 && s.resources >= bp.geo_bot_cost { 137 if m > 1 && s.resources >= bp.geo_bot_cost {
144 new_status.resources -= &bp.geo_bot_cost; 138 new_status.resources -= &bp.geo_bot_cost;
145 new_status.bots.geo += 1; 139 new_status.bots.geo += 1;
146 result = most_geodes(bp, new_status, m-1, mem, result); 140 result = most_geodes(bp, new_status, m-1, mem);
147 141
148 // If a geode bot can be built, it is always the best thing to do 142 // If a geode bot can be built, it is always the best thing to do
149 mem.insert((s, m), result); 143 mem.insert((s, m), result);
@@ -153,7 +147,7 @@ pub fn most_geodes(
153 if m > 2 && s.resources >= bp.obs_bot_cost { 147 if m > 2 && s.resources >= bp.obs_bot_cost {
154 new_status.resources -= &bp.obs_bot_cost; 148 new_status.resources -= &bp.obs_bot_cost;
155 new_status.bots.obs += 1; 149 new_status.bots.obs += 1;
156 result = max(result, most_geodes(bp, new_status, m-1, mem, result)); 150 result = max(result, most_geodes(bp, new_status, m-1, mem));
157 new_status.bots.obs -= 1; 151 new_status.bots.obs -= 1;
158 new_status.resources += &bp.obs_bot_cost; 152 new_status.resources += &bp.obs_bot_cost;
159 } 153 }
@@ -161,7 +155,7 @@ pub fn most_geodes(
161 if m > 3 && s.resources >= bp.cla_bot_cost { 155 if m > 3 && s.resources >= bp.cla_bot_cost {
162 new_status.resources -= &bp.cla_bot_cost; 156 new_status.resources -= &bp.cla_bot_cost;
163 new_status.bots.cla += 1; 157 new_status.bots.cla += 1;
164 result = max(result, most_geodes(bp, new_status, m-1, mem, result)); 158 result = max(result, most_geodes(bp, new_status, m-1, mem));
165 new_status.bots.cla -= 1; 159 new_status.bots.cla -= 1;
166 new_status.resources += &bp.cla_bot_cost; 160 new_status.resources += &bp.cla_bot_cost;
167 } 161 }
@@ -169,12 +163,12 @@ pub fn most_geodes(
169 if m > 4 && s.resources >= bp.ore_bot_cost { 163 if m > 4 && s.resources >= bp.ore_bot_cost {
170 new_status.resources -= &bp.ore_bot_cost; 164 new_status.resources -= &bp.ore_bot_cost;
171 new_status.bots.ore += 1; 165 new_status.bots.ore += 1;
172 result = max(result, most_geodes(bp, new_status, m-1, mem, result)); 166 result = max(result, most_geodes(bp, new_status, m-1, mem));
173 new_status.bots.ore -= 1; 167 new_status.bots.ore -= 1;
174 new_status.resources += &bp.ore_bot_cost; 168 new_status.resources += &bp.ore_bot_cost;
175 } 169 }
176 170
177 result = max(result, most_geodes(bp, new_status, m-1, mem, result)); 171 result = max(result, most_geodes(bp, new_status, m-1, mem));
178 172
179 mem.insert((s, m), result); 173 mem.insert((s, m), result);
180 result 174 result

Generated with cgit - Back to sebastiano.tronto.net