Skip to content

Commit ebac06e

Browse files
committed
Caches by sorted keys
1 parent cbf3b4e commit ebac06e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

2019/18/maze.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,23 @@ class Maze {
7878
* get unlocked.
7979
*/
8080
countSteps(from_id, to_coord, keys_collected) {
81+
const sorted_keys = keys_collected.split('').sort().join('');
8182
let current = to_coord;
8283
let count = 0;
83-
if (!this.count_cache[keys_collected]) {
84-
this.count_cache[keys_collected] = {};
84+
if (!this.count_cache[sorted_keys]) {
85+
this.count_cache[sorted_keys] = {};
8586
}
8687
const came_from = this.pathfinders.get(from_id);
8788
const cache_key = `${InfiniteGrid.toId(...current)}/${from_id}`;
88-
if (this.count_cache[keys_collected][cache_key] === undefined) {
89+
if (this.count_cache[sorted_keys][cache_key] === undefined) {
8990
while (InfiniteGrid.toId(...current) !== from_id) {
9091
count++;
9192
current = came_from.get(InfiniteGrid.toId(...current));
9293
}
93-
this.count_cache[keys_collected][cache_key] = count;
94+
this.count_cache[sorted_keys][cache_key] = count;
9495
}
9596

96-
return this.count_cache[keys_collected][cache_key];
97+
return this.count_cache[sorted_keys][cache_key];
9798
}
9899

99100
getReachableKeys(from_id, keys_collected) {

0 commit comments

Comments
 (0)