-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (57 loc) · 1.29 KB
/
index.js
File metadata and controls
70 lines (57 loc) · 1.29 KB
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
function canExit(maze) {
const envolve = new Array(maze.length + 2).fill('W')
maze = [
envolve,
...maze.map(row => ['W', ...row, 'W']),
envolve
]
const moves = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1]
]
function couldWeExit(maze, row, col) {
return (maze[row][col] === 'E') || (
(maze[row][col] === ' ') &&
(maze[row][col] = '.') &&
moves.some(([deltaRow, deltaCol]) =>
couldWeExit(maze, row + deltaRow, col + deltaCol)
)
)
}
const startRow = maze.findIndex(
columns => columns.find(c => c === 'S')
)
const startCol = maze[startRow].findIndex(
cell => cell === 'S'
)
maze[startRow][startCol] = ' '
const canExitNow = couldWeExit(
maze,
startRow,
startCol
)
return canExitNow
}
function canExitAlt(maze) {
function fillPath(line) {
return line
.join("")
.replace(/[S][\sE]/g, "SS")
.replace(/[\sE][S]/g, "SS")
.split("")
}
const x = maze[0].length
const y = maze.length
const area = x * y
new Array(area).fill('').forEach(() => {
maze.map((hor, i) => {
maze[i] = fillPath(hor)
maze[i] = maze[i]
.map((_, j) => fillPath(maze.map(x => x[j]))[i])
})
})
return !maze.flat(2).includes("E")
}
module.exports = canExit