-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.js
134 lines (120 loc) · 2.87 KB
/
12.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
/**
*
* @param {number[]} startXY
* @param {number[]} endXY
* @param {number[][]} grid
* @returns
*/
function bfs(startXY, endXY, grid) {
const startNode = startXY.join('-');
const endNode = endXY.join('-');
const queue = [startNode];
const came_from = new Map();
came_from.set(startNode, null);
while (queue.length > 0) {
const currentNode = queue.shift();
if (currentNode == endNode) break;
const [cx, cy] = currentNode.split('-').map(e => parseInt(e, 10));
let newNode = '';
// Search side paths
if (cx - 1 > -1 && (grid[cy][cx - 1] - grid[cy][cx]) < 2) {
newNode = (cx - 1) + '-' + cy;
if (!came_from.has(newNode)) {
queue.push(newNode);
came_from.set(newNode, currentNode);
}
}
if (cx + 1 < grid[0].length && (grid[cy][cx + 1] - grid[cy][cx]) < 2) {
newNode = (cx + 1) + '-' + cy;
if (!came_from.has(newNode)) {
queue.push(newNode);
came_from.set(newNode, currentNode);
}
}
if (cy - 1 > -1 && (grid[cy - 1][cx] - grid[cy][cx]) < 2) {
newNode = cx + '-' + (cy - 1);
if (!came_from.has(newNode)) {
queue.push(newNode);
came_from.set(newNode, currentNode);
}
}
if (cy + 1 < grid.length && (grid[cy + 1][cx] - grid[cy][cx]) < 2) {
newNode = cx + '-' + (cy + 1);
if (!came_from.has(newNode)) {
queue.push(newNode);
came_from.set(newNode, currentNode);
}
}
}
let currentNode = endNode;
const path = [];
while (currentNode != startNode) {
path.push(currentNode);
currentNode = came_from.get(currentNode);
if (!currentNode) return grid.length * grid[0].length;
}
return path.length;
}
/**
* @param {string} d
*/
export const part1 = async d => {
const data = d.split('\n').map(e => e.split(''));
const startXY = [];
const endXY = [];
/** @type {number[][]} */
const grid = [];
data.forEach((row, y) => {
const gridRow = [];
for (let x = 0; x < row.length; x++) {
if (row[x] == 'S') {
row[x] = 'a';
startXY.push(x);
startXY.push(y);
}
if (row[x] == 'E') {
row[x] = 'z';
endXY.push(x);
endXY.push(y);
}
const h = row[x].charCodeAt(0) - 97;
gridRow.push(h);
}
grid.push(gridRow);
});
return bfs(startXY, endXY, grid);
};
/**
* @param {string} d
*/
export const part2 = async d => {
const data = d.split('\n').map(e => e.split(''));
const startXY = [];
const endXY = [];
/** @type {number[][]} */
const grid = [];
data.forEach((row, y) => {
const gridRow = [];
for (let x = 0; x < row.length; x++) {
if (row[x] == 'S') {
row[x] = 'a';
}
if (row[x] == 'a') {
startXY.push([x, y]);
}
if (row[x] == 'E') {
row[x] = 'z';
endXY.push(x);
endXY.push(y);
}
const h = row[x].charCodeAt(0) - 97;
gridRow.push(h);
}
grid.push(gridRow);
});
let minSteps = grid.length * grid[0].length;
startXY.forEach(start => {
minSteps = Math.min(minSteps, bfs(start, endXY, grid));
});
return minSteps;
};