diff options
Diffstat (limited to '2022')
| -rw-r--r-- | 2022/19/common.rs | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/2022/19/common.rs b/2022/19/common.rs index 319363e..060d4fc 100644 --- a/2022/19/common.rs +++ b/2022/19/common.rs | |||
| @@ -9,14 +9,12 @@ use std::collections::HashMap; | |||
| 9 | pub struct Stock { | 9 | pub struct Stock { |
| 10 | pub ore: i32, | 10 | pub ore: i32, |
| 11 | pub cla: i32, | 11 | pub cla: i32, |
| 12 | pub obs: i32, | 12 | pub obs: i32 |
| 13 | pub geo: i32 | ||
| 14 | } | 13 | } |
| 15 | 14 | ||
| 16 | impl Stock { | 15 | impl Stock { |
| 17 | pub fn all_lower(&self, other: &Self) -> bool { | 16 | pub fn all_lower(&self, other: &Self) -> bool { |
| 18 | self.ore <= other.ore && self.cla <= other.cla && | 17 | self.ore <= other.ore && self.cla <= other.cla && self.obs <= other.obs |
| 19 | self.obs <= other.obs && self.geo <= other.geo | ||
| 20 | } | 18 | } |
| 21 | } | 19 | } |
| 22 | 20 | ||
| @@ -34,7 +32,6 @@ impl AddAssign<&Stock> for Stock { | |||
| 34 | self.ore += other.ore; | 32 | self.ore += other.ore; |
| 35 | self.cla += other.cla; | 33 | self.cla += other.cla; |
| 36 | self.obs += other.obs; | 34 | self.obs += other.obs; |
| 37 | self.geo += other.geo; | ||
| 38 | } | 35 | } |
| 39 | } | 36 | } |
| 40 | 37 | ||
| @@ -43,7 +40,6 @@ impl SubAssign<&Stock> for Stock { | |||
| 43 | self.ore -= other.ore; | 40 | self.ore -= other.ore; |
| 44 | self.cla -= other.cla; | 41 | self.cla -= other.cla; |
| 45 | self.obs -= other.obs; | 42 | self.obs -= other.obs; |
| 46 | self.geo -= other.geo; | ||
| 47 | } | 43 | } |
| 48 | } | 44 | } |
| 49 | 45 | ||
| @@ -56,8 +52,8 @@ pub struct Status { | |||
| 56 | impl Status { | 52 | impl Status { |
| 57 | pub fn new() -> Status { | 53 | pub fn new() -> Status { |
| 58 | Status { | 54 | Status { |
| 59 | bots: Stock { ore: 1, cla: 0, obs: 0, geo: 0 }, | 55 | bots: Stock { ore: 1, cla: 0, obs: 0 }, |
| 60 | resources: Stock { ore: 0, cla: 0, obs: 0, geo: 0 } | 56 | resources: Stock { ore: 0, cla: 0, obs: 0 } |
| 61 | } | 57 | } |
| 62 | } | 58 | } |
| 63 | } | 59 | } |
| @@ -93,16 +89,16 @@ impl Blueprint { | |||
| 93 | let mut i = 0; | 89 | let mut i = 0; |
| 94 | 90 | ||
| 95 | let ore = Blueprint::parse_one_resource(&line, &mut i); | 91 | let ore = Blueprint::parse_one_resource(&line, &mut i); |
| 96 | let ore_bot_cost = Stock { ore, cla: 0, obs: 0, geo: 0 }; | 92 | let ore_bot_cost = Stock { ore, cla: 0, obs: 0 }; |
| 97 | 93 | ||
| 98 | let ore = Blueprint::parse_one_resource(&line, &mut i); | 94 | let ore = Blueprint::parse_one_resource(&line, &mut i); |
| 99 | let cla_bot_cost = Stock { ore, cla: 0, obs: 0, geo: 0 }; | 95 | let cla_bot_cost = Stock { ore, cla: 0, obs: 0 }; |
| 100 | 96 | ||
| 101 | let (ore, cla) = Blueprint::parse_two_resources(&line, &mut i); | 97 | let (ore, cla) = Blueprint::parse_two_resources(&line, &mut i); |
| 102 | let obs_bot_cost = Stock { ore, cla, obs: 0, geo: 0 }; | 98 | let obs_bot_cost = Stock { ore, cla, obs: 0 }; |
| 103 | 99 | ||
| 104 | let (ore, obs) = Blueprint::parse_two_resources(&line, &mut i); | 100 | let (ore, obs) = Blueprint::parse_two_resources(&line, &mut i); |
| 105 | let geo_bot_cost = Stock { ore, cla: 0, obs, geo: 0 }; | 101 | let geo_bot_cost = Stock { ore, cla: 0, obs }; |
| 106 | 102 | ||
| 107 | Blueprint { ore_bot_cost, cla_bot_cost, obs_bot_cost, geo_bot_cost } | 103 | Blueprint { ore_bot_cost, cla_bot_cost, obs_bot_cost, geo_bot_cost } |
| 108 | } | 104 | } |
| @@ -124,27 +120,28 @@ pub fn most_geodes( | |||
| 124 | m: i32, | 120 | m: i32, |
| 125 | mem: &mut HashMap<(Status, i32), i32> | 121 | mem: &mut HashMap<(Status, i32), i32> |
| 126 | ) -> i32 { | 122 | ) -> i32 { |
| 127 | if m == 0 { return s.resources.geo; } | 123 | if m <= 1 { return 0; } |
| 128 | if m == 1 { return s.resources.geo + s.bots.geo; } | ||
| 129 | 124 | ||
| 130 | if let Some(r) = mem.get(&(s, m)) { return *r; } | 125 | if let Some(r) = mem.get(&(s, m)) { return *r; } |
| 131 | 126 | ||
| 132 | let mut new_status = s; | 127 | let mut new_status = s; |
| 133 | new_status.resources += &new_status.bots; | 128 | new_status.resources += &new_status.bots; |
| 134 | 129 | ||
| 135 | let mut result = 0; | 130 | // If a geode bot can be built, it is always the best thing to do |
| 136 | |||
| 137 | if m > 1 && s.resources >= bp.geo_bot_cost { | 131 | if m > 1 && s.resources >= bp.geo_bot_cost { |
| 138 | new_status.resources -= &bp.geo_bot_cost; | 132 | new_status.resources -= &bp.geo_bot_cost; |
| 139 | new_status.bots.geo += 1; | 133 | let result = m - 1 + most_geodes(bp, new_status, m-1, mem); |
| 140 | result = most_geodes(bp, new_status, m-1, mem); | ||
| 141 | |||
| 142 | // If a geode bot can be built, it is always the best thing to do | ||
| 143 | mem.insert((s, m), result); | 134 | mem.insert((s, m), result); |
| 144 | return result; | 135 | return result; |
| 145 | } | 136 | } |
| 146 | 137 | ||
| 147 | if m > 2 && s.resources >= bp.obs_bot_cost { | 138 | let mut result = 0; |
| 139 | |||
| 140 | let can_obs = s.resources >= bp.obs_bot_cost; | ||
| 141 | let can_cla = s.resources >= bp.cla_bot_cost; | ||
| 142 | let can_ore = s.resources >= bp.ore_bot_cost; | ||
| 143 | |||
| 144 | if m > 2 && can_obs { | ||
| 148 | new_status.resources -= &bp.obs_bot_cost; | 145 | new_status.resources -= &bp.obs_bot_cost; |
| 149 | new_status.bots.obs += 1; | 146 | new_status.bots.obs += 1; |
| 150 | result = max(result, most_geodes(bp, new_status, m-1, mem)); | 147 | result = max(result, most_geodes(bp, new_status, m-1, mem)); |
| @@ -152,7 +149,7 @@ pub fn most_geodes( | |||
| 152 | new_status.resources += &bp.obs_bot_cost; | 149 | new_status.resources += &bp.obs_bot_cost; |
| 153 | } | 150 | } |
| 154 | 151 | ||
| 155 | if m > 3 && s.resources >= bp.cla_bot_cost { | 152 | if m > 3 && can_cla { |
| 156 | new_status.resources -= &bp.cla_bot_cost; | 153 | new_status.resources -= &bp.cla_bot_cost; |
| 157 | new_status.bots.cla += 1; | 154 | new_status.bots.cla += 1; |
| 158 | result = max(result, most_geodes(bp, new_status, m-1, mem)); | 155 | result = max(result, most_geodes(bp, new_status, m-1, mem)); |
| @@ -160,7 +157,7 @@ pub fn most_geodes( | |||
| 160 | new_status.resources += &bp.cla_bot_cost; | 157 | new_status.resources += &bp.cla_bot_cost; |
| 161 | } | 158 | } |
| 162 | 159 | ||
| 163 | if m > 4 && s.resources >= bp.ore_bot_cost { | 160 | if m > 4 && can_ore { |
| 164 | new_status.resources -= &bp.ore_bot_cost; | 161 | new_status.resources -= &bp.ore_bot_cost; |
| 165 | new_status.bots.ore += 1; | 162 | new_status.bots.ore += 1; |
| 166 | result = max(result, most_geodes(bp, new_status, m-1, mem)); | 163 | result = max(result, most_geodes(bp, new_status, m-1, mem)); |
| @@ -168,7 +165,9 @@ pub fn most_geodes( | |||
| 168 | new_status.resources += &bp.ore_bot_cost; | 165 | new_status.resources += &bp.ore_bot_cost; |
| 169 | } | 166 | } |
| 170 | 167 | ||
| 171 | result = max(result, most_geodes(bp, new_status, m-1, mem)); | 168 | if !can_obs || !can_cla || !can_ore { |
| 169 | result = max(result, most_geodes(bp, new_status, m-1, mem)); | ||
| 170 | } | ||
| 172 | 171 | ||
| 173 | mem.insert((s, m), result); | 172 | mem.insert((s, m), result); |
| 174 | result | 173 | result |
