-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.ts
53 lines (46 loc) · 1.42 KB
/
part2.ts
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
import * as fs from 'fs';
const input = fs.readFileSync('input', 'utf8').split('\n');
const starts = [];
let end = { x: 0, y: 0, e: 0 };
const maze = input.map((line, row) =>
line.split('').map((cell, column) => {
if (cell === 'S' || cell === 'a') {
let start = { x: column, y: row, e: 'a'.charCodeAt(0) };
starts.push(start);
return start;
} else if (cell === 'E') {
return (end = { x: column, y: row, e: 'z'.charCodeAt(0) });
} else {
return { x: column, y: row, e: cell.charCodeAt(0) };
}
}),
);
const findMinSteps = (start) => {
const queue = [{ ...start, steps: 0 }];
const visited = new Set(`${start.x},${start.y}}`);
while (queue.length > 0) {
const current = queue.shift();
if (current.x === end.x && current.y === end.y) {
return current.steps;
break;
}
const neighbors = [
maze[current.y - 1] && maze[current.y - 1][current.x],
maze[current.y + 1] && maze[current.y + 1][current.x],
maze[current.y][current.x - 1],
maze[current.y][current.x + 1],
]
.filter((cell) => cell && !visited.has(`${cell.x},${cell.y}`))
.filter((cell) => cell.e - current.e <= 1)
.map((cell) => ({ ...cell, steps: current.steps + 1 }));
neighbors.forEach((cell) => visited.add(`${cell.x},${cell.y}`));
queue.push(...neighbors);
}
return Infinity;
};
let min = Infinity;
starts.forEach((start) => {
const result = findMinSteps(start);
min = result < min ? result : min;
});
console.log(min);