diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-06 09:48:32 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-06 09:48:32 +0200 |
| commit | 58800a74af2c9f3578d3e62b3226aaf7c73aef87 (patch) | |
| tree | a34e75378d99a16196fc95fb23e2d5d80f22e1bb /2022 | |
| parent | 950d82569ddf137f1674095bfbe6bfff8fce663f (diff) | |
| download | aoc-58800a74af2c9f3578d3e62b3226aaf7c73aef87.tar.gz aoc-58800a74af2c9f3578d3e62b3226aaf7c73aef87.zip | |
Finally, part 2 for Day 19 2022
Diffstat (limited to '2022')
| -rw-r--r-- | 2022/19/a.rs | 2 | ||||
| -rw-r--r-- | 2022/19/b.rs | 17 | ||||
| -rw-r--r-- | 2022/19/common.rs | 20 |
3 files changed, 31 insertions, 8 deletions
diff --git a/2022/19/a.rs b/2022/19/a.rs index 898debd..371d356 100644 --- a/2022/19/a.rs +++ b/2022/19/a.rs | |||
| @@ -4,7 +4,7 @@ use common::*; | |||
| 4 | 4 | ||
| 5 | fn main() { | 5 | fn main() { |
| 6 | const MINUTES: i32 = 24; | 6 | const MINUTES: i32 = 24; |
| 7 | let mut mem = HashMap::<(Status, i32), i32>::new(); | 7 | let mut mem = HashMap::<(Status, i32), i64>::new(); |
| 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() { |
diff --git a/2022/19/b.rs b/2022/19/b.rs new file mode 100644 index 0000000..d97bf53 --- /dev/null +++ b/2022/19/b.rs | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | use std::collections::HashMap; | ||
| 2 | mod common; | ||
| 3 | use common::*; | ||
| 4 | |||
| 5 | fn main() { | ||
| 6 | const MINUTES: i32 = 32; | ||
| 7 | let mut mem = HashMap::<(Status, i32), i64>::new(); | ||
| 8 | let mut prod = 1; | ||
| 9 | let bp = read_blueprints_from_stdin(); | ||
| 10 | for i in 0..3 { | ||
| 11 | let mg = most_geodes(&bp[i], &Status::new(), MINUTES, &mut mem); | ||
| 12 | println!("{i}: {mg}"); | ||
| 13 | prod *= mg; | ||
| 14 | mem.clear(); | ||
| 15 | } | ||
| 16 | println!("{prod}"); | ||
| 17 | } | ||
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 | ||
| 69 | impl Blueprint { | 69 | impl 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; |
