aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-07-06 00:18:18 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-07-06 00:18:18 +0200
commit950d82569ddf137f1674095bfbe6bfff8fce663f (patch)
treecfa15729352f078e35c3c6c5afbcc5f3d220842f
parent2b5e220841acc8aa48e403e8faf4a29b161bca8a (diff)
downloadaoc-950d82569ddf137f1674095bfbe6bfff8fce663f.tar.gz
aoc-950d82569ddf137f1674095bfbe6bfff8fce663f.zip
Minor improvement using ref instead of copy
-rw-r--r--2022/19/a.rs2
-rw-r--r--2022/19/common.rs20
2 files changed, 11 insertions, 11 deletions
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() {
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); 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 060d4fc..95b9a2d 100644
--- a/2022/19/common.rs
+++ b/2022/19/common.rs
@@ -116,22 +116,22 @@ pub fn read_blueprints_from_stdin() -> Vec<Blueprint> {
116 116
117pub fn most_geodes( 117pub 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), i32>
122) -> i32 { 122) -> i32 {
123 if m <= 1 { return 0; } 123 if m <= 1 { return 0; }
124 124
125 if let Some(r) = mem.get(&(s, m)) { return *r; } 125 if let Some(r) = mem.get(&(s.clone(), m)) { return *r; }
126 126
127 let mut new_status = s; 127 let mut new_status = s.clone();
128 new_status.resources += &new_status.bots; 128 new_status.resources += &new_status.bots;
129 129
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 + most_geodes(bp, &new_status, m-1, mem);
134 mem.insert((s, m), result); 134 mem.insert((s.clone(), m), result);
135 return result; 135 return result;
136 } 136 }
137 137
@@ -144,7 +144,7 @@ pub fn most_geodes(
144 if m > 2 && can_obs { 144 if m > 2 && can_obs {
145 new_status.resources -= &bp.obs_bot_cost; 145 new_status.resources -= &bp.obs_bot_cost;
146 new_status.bots.obs += 1; 146 new_status.bots.obs += 1;
147 result = max(result, most_geodes(bp, new_status, m-1, mem)); 147 result = max(result, most_geodes(bp, &new_status, m-1, mem));
148 new_status.bots.obs -= 1; 148 new_status.bots.obs -= 1;
149 new_status.resources += &bp.obs_bot_cost; 149 new_status.resources += &bp.obs_bot_cost;
150 } 150 }
@@ -152,7 +152,7 @@ pub fn most_geodes(
152 if m > 3 && can_cla { 152 if m > 3 && can_cla {
153 new_status.resources -= &bp.cla_bot_cost; 153 new_status.resources -= &bp.cla_bot_cost;
154 new_status.bots.cla += 1; 154 new_status.bots.cla += 1;
155 result = max(result, most_geodes(bp, new_status, m-1, mem)); 155 result = max(result, most_geodes(bp, &new_status, m-1, mem));
156 new_status.bots.cla -= 1; 156 new_status.bots.cla -= 1;
157 new_status.resources += &bp.cla_bot_cost; 157 new_status.resources += &bp.cla_bot_cost;
158 } 158 }
@@ -160,15 +160,15 @@ pub fn most_geodes(
160 if m > 4 && can_ore { 160 if m > 4 && can_ore {
161 new_status.resources -= &bp.ore_bot_cost; 161 new_status.resources -= &bp.ore_bot_cost;
162 new_status.bots.ore += 1; 162 new_status.bots.ore += 1;
163 result = max(result, most_geodes(bp, new_status, m-1, mem)); 163 result = max(result, most_geodes(bp, &new_status, m-1, mem));
164 new_status.bots.ore -= 1; 164 new_status.bots.ore -= 1;
165 new_status.resources += &bp.ore_bot_cost; 165 new_status.resources += &bp.ore_bot_cost;
166 } 166 }
167 167
168 if !can_obs || !can_cla || !can_ore { 168 if !can_obs || !can_cla || !can_ore {
169 result = max(result, most_geodes(bp, new_status, m-1, mem)); 169 result = max(result, most_geodes(bp, &new_status, m-1, mem));
170 } 170 }
171 171
172 mem.insert((s, m), result); 172 mem.insert((s.clone(), m), result);
173 result 173 result
174} 174}

Generated with cgit - Back to sebastiano.tronto.net