-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution_2016_22.rs
356 lines (298 loc) · 9.76 KB
/
solution_2016_22.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::str::FromStr;
use advent_of_code_common::coords2d::Coords2D;
use advent_of_code_common::parsing::Error;
use advent_of_code_common::utils::single_result;
use itertools::Itertools;
use pathfinding::matrix::Matrix;
use pathfinding::prelude::astar;
use regex::Regex;
#[derive(Debug)]
struct NodeInformation {
location: Coords2D<usize>,
capacity: u16,
used: u16,
}
impl FromStr for NodeInformation {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let re = Regex::new(r"/dev/grid/node-x(\d+)-y(\d+)\s+(\d+)T\s+(\d+)T\s+(\d+)T\s+(\d+)%")
.map_err(|_| "Error while compiling regex".to_string())?;
let caps = re.captures(s).ok_or("No captures for regex".to_string())?;
let get_group = |group: usize| -> Result<String, Error> {
caps.get(group)
.map(|m| m.as_str().to_owned())
.ok_or(format!("Could not get capture group {group}"))
};
let parse_int = |s: &str| -> Result<u16, Error> {
s.parse::<u16>()
.map_err(|_| "Failed to parse integer".to_string())
};
let x = parse_int(&get_group(1)?)?;
let y = parse_int(&get_group(2)?)?;
let size = parse_int(&get_group(3)?)?;
let used = parse_int(&get_group(4)?)?;
Ok(NodeInformation {
location: Coords2D {
x: x as usize,
y: y as usize,
},
capacity: size,
used,
})
}
}
type ParsedInputData = Vec<NodeInformation>;
fn parse(input: &str) -> Result<ParsedInputData, Error> {
let mut lines = input.lines();
let l1 = lines.next().ok_or("Missing Line 1")?;
assert_eq!(l1, "root@ebhq-gridcenter# df -h");
let l2 = lines.next().ok_or("Missing Line 2")?;
assert_eq!(l2, "Filesystem Size Used Avail Use%");
let mut result = Vec::new();
for line in lines {
let node = NodeInformation::from_str(line)?;
result.push(node);
}
Ok(result)
}
fn solve_1(data: &ParsedInputData) -> usize {
let (capacities, state) = create(data);
state.viable_moves_ignoring_proximity(&capacities).len()
}
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
struct CompleteState {
goal_at: (usize, usize),
usages: Matrix<u16>,
}
impl CompleteState {
fn pretty_print(&self, capacities: &Matrix<u16>) -> String {
(0 .. self.usages.rows)
.map(|row| {
(0 .. self.usages.columns)
.map(|column| {
let key = (row, column);
let capacity = capacities[key];
let usage = self.usages[key];
format!("{usage:>3}/{capacity:>3}")
})
.join(" | ")
})
.join("\n")
}
fn is_target(&self) -> bool {
self.goal_at == (0, 0)
}
fn viable_move(
&self,
capacities: &Matrix<u16>,
from: (usize, usize),
to: (usize, usize),
) -> bool {
let from_used = self.usages[from];
let to_used = self.usages[to];
let to_capacity = capacities[to];
from_used != 0 && // Node A is not empty (its Used is not zero)
from != to && // Nodes A and B are not the same node
from_used <= to_capacity - to_used // The data on node A (its Used) would fit on node B (its Avail)
}
fn viable_moves_ignoring_proximity(
&self,
capacities: &Matrix<u16>,
) -> Vec<((usize, usize), (usize, usize))> {
let mut result = vec![];
for from in self.usages.keys() {
for to in self.usages.keys() {
if self.viable_move(capacities, from, to) {
result.push((from, to));
}
}
}
result
}
fn viable_neighbouring_moves(
&self,
capacities: &Matrix<u16>,
) -> Vec<((usize, usize), (usize, usize))> {
let mut result = vec![];
for from in self.usages.keys() {
for to in self.usages.neighbours(from, false) {
if self.viable_move(capacities, from, to) {
result.push((from, to));
}
}
}
result
}
fn successors(&self, capacities: &Matrix<u16>) -> Vec<(CompleteState, usize)> {
self.viable_neighbouring_moves(capacities)
.into_iter()
.map(|(from, to)| {
let goal_at = if self.goal_at == from {
to
} else {
self.goal_at
};
let mut usages = self.usages.clone();
let moving = usages[from];
usages[from] = 0;
usages[to] += moving;
debug_assert_eq!(
self.usages[from] + self.usages[to],
usages[from] + usages[to]
);
(Self { goal_at, usages }, 1)
})
.collect()
}
fn heuristic(&self) -> usize {
let (y, x) = self.goal_at;
x + y
}
}
fn create(data: &ParsedInputData) -> (Matrix<u16>, CompleteState) {
let max_x = data.iter().map(|n| n.location.x).max().unwrap();
let max_y = data.iter().map(|n| n.location.y).max().unwrap();
let matrix_from_nodes = |f: fn(&NodeInformation) -> u16| {
let mut m: Matrix<u16> = Matrix::new(max_y + 1, max_x + 1, 0);
for node in data {
m[node.location.as_tuple()] = f(node);
}
m
};
let capacities: Matrix<u16> = matrix_from_nodes(|node| node.capacity);
let usages: Matrix<u16> = matrix_from_nodes(|node| node.used);
let state = CompleteState {
goal_at: (0, max_x),
usages,
};
(capacities, state)
}
// This takes too long for larger levels
#[allow(unused)]
fn solve_2_using_complete_state(data: &ParsedInputData) -> Option<usize> {
let (capacities, complete_state) = create(data);
println!(
"Complete starting state:\n{}",
complete_state.pretty_print(&capacities)
);
astar(
&complete_state,
|state| state.successors(&capacities),
CompleteState::heuristic,
CompleteState::is_target,
)
.map(|(_, result)| result)
}
#[derive(Eq, PartialEq, Hash, Clone, Debug)]
struct SimplifiedState {
goal_at: (usize, usize),
empty_at: (usize, usize),
}
impl SimplifiedState {
fn is_target(&self) -> bool {
self.goal_at == (0, 0)
}
fn heuristic(&self) -> usize {
let (y, x) = self.goal_at;
x + y
}
fn successors(
&self,
capacities: &Matrix<u16>,
large_threshold: u16,
) -> Vec<(SimplifiedState, usize)> {
capacities
.neighbours(self.empty_at, false)
.filter(|c| capacities[*c] < large_threshold)
.map(|c| {
let goal_at = if c == self.goal_at {
self.empty_at
} else {
self.goal_at
};
let empty_at = c;
(Self { goal_at, empty_at }, 1)
})
.collect()
}
fn pretty_print(&self, capacities: &Matrix<u16>, large_threshold: u16) -> String {
(0 .. capacities.rows)
.map(|row| {
(0 .. capacities.columns)
.map(|column| {
let key = (row, column);
let repr = if capacities[key] >= large_threshold {
"#"
} else if key == self.empty_at {
"_"
} else if key == self.goal_at {
"G"
} else {
"."
};
format!(" {repr} ")
})
.join("")
})
.join("\n")
}
}
fn solve_2_using_simplified_state(data: &ParsedInputData, large_threshold: u16) -> Option<usize> {
let (capacities, complete_state) = create(data);
let empty_coords: Vec<_> = complete_state
.usages
.keys()
.filter(|k| complete_state.usages[*k] == 0)
.collect();
let empty_at = *single_result(&empty_coords).unwrap();
let start_state = SimplifiedState {
goal_at: complete_state.goal_at,
empty_at,
};
println!(
"Starting state:\n{}\n",
start_state.pretty_print(&capacities, large_threshold)
);
astar(
&start_state,
|state| state.successors(&capacities, large_threshold),
SimplifiedState::heuristic,
SimplifiedState::is_target,
)
.map(|(_, result)| result)
}
fn part_1(input: &str) -> Result<usize, Error> {
parse(input).map(|input| solve_1(&input))
}
// `large_threshold` parameter is currently a heuristic, found through viewing the printed output
// of `CompleteState`. It can probably be calculated, but it was not needed.
fn part_2(input: &str, large_threshold: u16) -> Result<Option<usize>, Error> {
parse(input).map(|input| solve_2_using_simplified_state(&input, large_threshold))
}
const DATA: &str = include_str!("../../resources/22.txt");
fn main() -> Result<(), Error> {
let result_1 = part_1(DATA)?;
println!("Part 1: {result_1}");
let result_2 = part_2(DATA, 450)?;
println!("Part 2: {result_2:?}");
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = include_str!("../../resources/22-test.txt");
#[test]
#[ignore]
fn test_solve_1_real() {
assert_eq!(part_1(DATA), Ok(901));
}
#[test]
fn test_solve_2_test() {
assert_eq!(part_2(TEST, 20), Ok(Some(7)));
}
#[test]
#[ignore]
fn test_solve_2_real() {
assert_eq!(part_2(DATA, 450), Ok(Some(238)));
}
}