Skip to content

Commit 8d726d6

Browse files
committed
Implements new clever pruning method
After I had both solutions working, I read the thread and the top comment - https://www.reddit.com/r/adventofcode/comments/ec8090/2019_day_18_solutions/fhiwgh4/ mentioned that they pruned similarly to me, *but also* didn't use the steps as a key, but rather compared steps and pruned longer paths, which is really smart. Now everything runs very quickly.
1 parent 613481c commit 8d726d6

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

2019/18/maze.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ class Maze {
203203
}
204204

205205
/**
206-
* If we have identical robot positions, steps, and keys collected,
207-
* consider those paths as duplicates and prune them.
206+
* If we have identical robot positions, and keys collected,
207+
* then store the path with fewer steps.
208208
*/
209209
let pruned_paths = new Map();
210210
for (let path of new_paths) {
@@ -213,10 +213,16 @@ class Maze {
213213
path.at_end = true;
214214
}
215215
const sorted_keys_str = path.keys_collected.split('').sort().join('');
216-
const path_id = `${JSON.stringify(path.robots_coords)};${
217-
path.steps
218-
};${sorted_keys_str}`;
219-
pruned_paths.set(path_id, path);
216+
const path_id = `${JSON.stringify(path.robots_coords)};${sorted_keys_str}`;
217+
if (pruned_paths.has(path_id)) {
218+
// Don't store path if it is longer than current stored path
219+
if (pruned_paths.get(path_id, path).steps > path.steps) {
220+
pruned_paths.set(path_id, path);
221+
}
222+
} else {
223+
// Haven't seen this configuration before, store it
224+
pruned_paths.set(path_id, path);
225+
}
220226
}
221227

222228
paths = [...pruned_paths.values()].sort((a, b) => a.steps - b.steps);

0 commit comments

Comments
 (0)