-
Notifications
You must be signed in to change notification settings - Fork 17
/
cave.js
195 lines (160 loc) · 5.98 KB
/
cave.js
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
const colors = require('colors');
const distance = require('manhattan');
const jsnx = require('jsnetworkx');
const { intersection } = require('lodash');
const ROCKY = 0;
const WET = 1;
const NARROW = 2;
const NEITHER = 0;
const GEAR = 1;
const TORCH = 2;
let TYPES_LOOKUP = {
[ROCKY]: '.',
[WET]: '=',
[NARROW]: '|',
};
class Cave {
constructor(depth, target_coords, x_padding = 40, y_padding = 1) {
/**
* Originally has memory issues when creating a grid that was `depth by depth`
* size, but was able to fix that with.
*
* node --max_old_space_size=4096 ./part-one.js`
*
* However, I don't actually need the full grid, so
* I'm creating a grid of largest by largest, with some
* buffer / padding around the edges.
*
*/
// let grid_size = target_coords[0] + target_coords[1] + 2;
// let grid_size = depth;
// let grid_size = Math.max(...target_coords) + 50;
this.depth = depth;
this.target_x = target_coords[0];
this.target_y = target_coords[1];
this.grid = Array(this.target_y + y_padding)
.fill()
.map(row => Array(this.target_x + x_padding).fill());
this.fillGrid();
this.directedGraph = this.createDirectedGraphFromGrid();
}
allowedTools(x, y) {
let { type } = this.grid[y][x];
if (type === ROCKY) return [GEAR, TORCH];
if (type === WET) return [NEITHER, GEAR];
if (type === NARROW) return [NEITHER, TORCH];
}
getValidNeighborCoords(x, y) {
let left = x > 0 ? [x - 1, y] : null,
right = x < this.grid[0].length - 1 ? [x + 1, y] : null,
up = y > 0 ? [x, y - 1] : null,
down = y < this.grid.length - 1 ? [x, y + 1] : null;
return [left, right, up, down].filter(n => n);
}
fillGrid() {
// Zig zag walk
for (let y = 0; y < this.grid.length; y++) {
for (let x = 0; x < this.grid[0].length; x++) {
// console.log(`${x},${y}`);
let index;
if (x === 0 && y === 0) {
index = 0;
} else if (x === this.target_x && y === this.target_y) {
index = 0;
} else if (y === 0) {
index = x * 16807;
} else if (x === 0) {
index = y * 48271;
} else {
index = this.grid[y - 1][x].erosion * this.grid[y][x - 1].erosion;
}
let erosion = (index + this.depth) % 20183;
let type = erosion % 3;
this.grid[y][x] = {
index,
erosion,
type,
};
}
}
}
createDirectedGraphFromGrid() {
const G = new jsnx.DiGraph();
for (let y = 0; y < this.grid.length; y++) {
for (let x = 0; x < this.grid[0].length; x++) {
// First, add "switching tools" edge
let allowed_tools = this.allowedTools(x, y);
let [tool_a, tool_b] = allowed_tools;
// Switch tools (moving in third dimension) has a weight of 7
G.addEdge(`${x},${y},${tool_a}`, `${x},${y},${tool_b}`, { weight: 7 });
G.addEdge(`${x},${y},${tool_b}`, `${x},${y},${tool_a}`, { weight: 7 });
// Next, add regular movement between neighbors where it is allowed
let neighbors = this.getValidNeighborCoords(x, y);
neighbors.forEach(neighbor => {
let [neighbor_x, neighbor_y] = neighbor;
//
let valid_tools_between_current_and_neighbor = intersection(
allowed_tools,
this.allowedTools(neighbor_x, neighbor_y)
);
valid_tools_between_current_and_neighbor.forEach(tool => {
G.addEdge(`${x},${y},${tool}`, `${neighbor_x},${neighbor_y},${tool}`, {
weight: 1,
});
});
});
}
}
return G;
}
getSumOfTypesFromOriginToTarget() {
let sum = 0;
for (let y = 0; y <= this.target_y; y++) {
for (let x = 0; x <= this.target_x; x++) {
let cell = this.grid[y][x];
sum += cell.type;
}
}
return sum;
}
printGrid(size_x, size_y) {
size_x = size_x === undefined ? this.grid[0].length : size_x;
size_y = size_y === undefined ? this.grid.length : size_y;
let grid_str = '';
for (let y = 0; y < size_y; y++) {
for (let x = 0; x < size_x; x++) {
let cell = this.grid[y][x];
if (x === 0 && y === 0) {
grid_str += 'M';
} else if (x === this.target_x && y === this.target_y) {
grid_str += 'T';
} else {
grid_str += TYPES_LOOKUP[cell.type];
}
}
grid_str += '\n';
}
return grid_str;
}
getShortestPathLengthToTarget() {
return jsnx.dijkstraPathLength(this.directedGraph, {
// From 0,0 with Torch equipped, to our target, also with the torch equipped
source: `0,0,${TORCH}`,
target: `${this.target_x},${this.target_y},${TORCH}`,
});
}
getShortestPathToTarget() {
return jsnx.dijkstraPath(this.directedGraph, {
// From `0,0 with Torch equipped`, to our `target, also with the torch equipped`
source: `0,0,${TORCH}`,
target: `${this.target_x},${this.target_y},${TORCH}`,
});
}
getGraphNodes(optData = false) {
return this.directedGraph.nodes(optData);
}
getGraphEdges(optData) {
return this.directedGraph.edges(optData);
}
}
module.exports = Cave;