generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.rs
284 lines (244 loc) · 8.67 KB
/
19.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
advent_of_code::solution!(19);
use regex::Regex;
use std::collections::HashSet;
use std::collections::VecDeque;
use advent_of_code::majcn::parse::ParseRegex;
struct Blueprint {
id: u32,
ore_robot_cost_ore: u32,
clay_robot_cost_ore: u32,
obsidian_robot_cost_ore: u32,
obsidian_robot_cost_clay: u32,
geode_robot_cost_ore: u32,
geode_robot_cost_obsidian: u32,
max_ore_cost: u32,
max_clay_cost: u32,
max_obsidian_cost: u32,
}
fn parse_data(input: &str) -> Vec<Blueprint> {
let mut regex_string = String::new();
regex_string += r"^Blueprint (\d+): ";
regex_string += r"Each ore robot costs (\d+) ore. ";
regex_string += r"Each clay robot costs (\d+) ore. ";
regex_string += r"Each obsidian robot costs (\d+) ore and (\d+) clay. ";
regex_string += r"Each geode robot costs (\d+) ore and (\d+) obsidian.$";
let re = Regex::new(®ex_string).unwrap();
input
.lines()
.map(|x| re.parse_u32::<7>(x))
.map(|[id, ore_robot_cost_ore, clay_robot_cost_ore, obsidian_robot_cost_ore, obsidian_robot_cost_clay, geode_robot_cost_ore, geode_robot_cost_obsidian]| {
let max_obsidian_cost = geode_robot_cost_obsidian;
let max_clay_cost = obsidian_robot_cost_clay;
let max_ore_cost = [ore_robot_cost_ore, clay_robot_cost_ore, obsidian_robot_cost_ore, geode_robot_cost_ore].into_iter().max().unwrap();
Blueprint {
id,
ore_robot_cost_ore,
clay_robot_cost_ore,
obsidian_robot_cost_ore,
obsidian_robot_cost_clay,
geode_robot_cost_ore,
geode_robot_cost_obsidian,
max_ore_cost,
max_clay_cost,
max_obsidian_cost,
}
})
.collect()
}
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash)]
struct State {
time: u32,
ore: u32,
clay: u32,
obsidian: u32,
geode: u32,
ore_robots: u32,
clay_robots: u32,
obsidian_robots: u32,
geode_robots: u32,
}
fn get_next_states(blueprint: &Blueprint, state: &State) -> Vec<State> {
if state.ore >= blueprint.geode_robot_cost_ore
&& state.obsidian >= blueprint.geode_robot_cost_obsidian
{
return vec![State {
time: state.time + 1,
ore: state.ore + state.ore_robots - blueprint.geode_robot_cost_ore,
clay: state.clay + state.clay_robots,
obsidian: state.obsidian + state.obsidian_robots - blueprint.geode_robot_cost_obsidian,
geode: state.geode + state.geode_robots,
ore_robots: state.ore_robots,
clay_robots: state.clay_robots,
obsidian_robots: state.obsidian_robots,
geode_robots: state.geode_robots + 1,
}];
}
if state.obsidian_robots < blueprint.max_obsidian_cost
&& state.ore >= blueprint.obsidian_robot_cost_ore
&& state.clay >= blueprint.obsidian_robot_cost_clay
{
return vec![State {
time: state.time + 1,
ore: state.ore + state.ore_robots - blueprint.obsidian_robot_cost_ore,
clay: state.clay + state.clay_robots - blueprint.obsidian_robot_cost_clay,
obsidian: state.obsidian + state.obsidian_robots,
geode: state.geode + state.geode_robots,
ore_robots: state.ore_robots,
clay_robots: state.clay_robots,
obsidian_robots: state.obsidian_robots + 1,
geode_robots: state.geode_robots,
}];
}
let mut result = Vec::with_capacity(3);
if state.clay_robots < blueprint.max_clay_cost && state.ore >= blueprint.clay_robot_cost_ore {
result.push(State {
time: state.time + 1,
ore: state.ore + state.ore_robots - blueprint.clay_robot_cost_ore,
clay: state.clay + state.clay_robots,
obsidian: state.obsidian + state.obsidian_robots,
geode: state.geode + state.geode_robots,
ore_robots: state.ore_robots,
clay_robots: state.clay_robots + 1,
obsidian_robots: state.obsidian_robots,
geode_robots: state.geode_robots,
});
}
if state.ore_robots < blueprint.max_ore_cost && state.ore >= blueprint.ore_robot_cost_ore {
result.push(State {
time: state.time + 1,
ore: state.ore + state.ore_robots - blueprint.ore_robot_cost_ore,
clay: state.clay + state.clay_robots,
obsidian: state.obsidian + state.obsidian_robots,
geode: state.geode + state.geode_robots,
ore_robots: state.ore_robots + 1,
clay_robots: state.clay_robots,
obsidian_robots: state.obsidian_robots,
geode_robots: state.geode_robots,
});
}
result.push(State {
time: state.time + 1,
ore: state.ore + state.ore_robots,
clay: state.clay + state.clay_robots,
obsidian: state.obsidian + state.obsidian_robots,
geode: state.geode + state.geode_robots,
ore_robots: state.ore_robots,
clay_robots: state.clay_robots,
obsidian_robots: state.obsidian_robots,
geode_robots: state.geode_robots,
});
result
}
fn sum_natural_numbers(a: i32, d: i32, n: i32) -> i32 {
n * (2 * a + (n - 1) * d) / 2
}
fn can_be_discarded(
state: &State,
best_result: &State,
blueprint: &Blueprint,
max_time: u32,
) -> bool {
let diff_time = max_time as i32 - state.time as i32;
let calculated_state_clay =
state.clay as i32 + sum_natural_numbers(state.clay_robots as i32, 1, diff_time);
let max_additional_obsidian_robots = i32::min(
calculated_state_clay / blueprint.obsidian_robot_cost_clay as i32,
diff_time,
);
let calculated_state_obsidian = state.obsidian as i32
+ sum_natural_numbers(
state.obsidian_robots as i32,
1,
max_additional_obsidian_robots,
)
+ (diff_time - max_additional_obsidian_robots)
* (state.obsidian_robots as i32 + max_additional_obsidian_robots);
let max_additional_geode_robots = i32::min(
calculated_state_obsidian / blueprint.geode_robot_cost_obsidian as i32,
diff_time,
);
let calculated_state_geode = state.geode as i32
+ sum_natural_numbers(state.geode_robots as i32, 1, max_additional_geode_robots)
+ (diff_time - max_additional_geode_robots)
* (state.geode_robots as i32 + max_additional_geode_robots);
calculated_state_geode <= best_result.geode as i32
}
struct Dfs {
init_state: State,
blueprint: Blueprint,
max_time: u32,
best_result: State,
discovered: HashSet<State>,
}
impl Dfs {
fn find_best(&mut self) -> State {
let mut queue = VecDeque::new();
queue.push_front(self.init_state);
while let Some(current_state) = queue.pop_front() {
self.discovered.insert(current_state);
for n in get_next_states(&self.blueprint, ¤t_state)
.into_iter()
.rev()
{
if self.discovered.contains(&n) {
continue;
}
if n.time == self.max_time {
if self.best_result.geode < n.geode {
self.best_result = n;
}
continue;
}
if can_be_discarded(&n, &self.best_result, &self.blueprint, self.max_time) {
continue;
}
queue.push_front(n);
}
}
self.best_result
}
}
fn find_max_geodes<const MAX_TIME: u32>(blueprint: Blueprint) -> u32 {
let mut dfs = Dfs {
init_state: State {
ore_robots: 1,
..Default::default()
},
blueprint,
max_time: MAX_TIME,
best_result: State::default(),
discovered: HashSet::new(),
};
dfs.find_best().geode
}
pub fn part_one(input: &str) -> Option<u32> {
let data = parse_data(input);
let result = data
.into_iter()
.map(|b| b.id * find_max_geodes::<24>(b))
.sum();
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let data = parse_data(input);
let result = data
.into_iter()
.take(3)
.map(find_max_geodes::<32>)
.product();
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(23));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(29348));
}
}