-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrush_hour.js
216 lines (199 loc) · 6.66 KB
/
rush_hour.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
let initial_board = [
[2, 2, 2, 0, 0, 3],
[0, 0, 4, 0, 0, 3],
[1, 1, 4, 0, 0, 3],
[5, 0, 4, 0, 6, 6],
[5, 0, 0, 0, 7, 0],
[8, 8, 8, 0, 7, 0],
]
const MAX_ARR_INDEX = 5
function isGoal(board) {
// console.log('isGoal', board)
return board[2][4] == 1 && board[2][5] == 1
}
function prettyPrintBoard(b) {
let string = ''
b.forEach((row) => {
row.forEach((column) => {
string += `${column} `
})
string += '\n'
})
console.log(string)
}
console.log('initial_board')
prettyPrintBoard(initial_board)
function getPossibleNextSteps(board) {
let car_searched = {} // {id:true}
let steps = [] // [{carId:left}]
board.forEach((row, x) => {
row.forEach((carId, y) => {
if (carId == 0 || car_searched[carId]) return
car_searched[carId] = true
// Search surrounding for car, from 0,0
// Search right
let rightCoordinate = y + 1
if (
rightCoordinate <= MAX_ARR_INDEX &&
board[x][rightCoordinate] == carId
) {
// can car move left?
let leftCoordindate = y - 1
if (leftCoordindate >= 0 && board[x][leftCoordindate] == 0) {
steps.push({ [carId]: 'left' })
}
// can car move right?
let moveRightCoordinate = rightCoordinate + 1
// 3 coordinate car
if (
moveRightCoordinate <= MAX_ARR_INDEX &&
board[x][moveRightCoordinate] == carId
) {
moveRightCoordinate++
}
if (
moveRightCoordinate <= MAX_ARR_INDEX &&
board[x][moveRightCoordinate] == 0
) {
steps.push({ [carId]: 'right' })
}
}
// Search down
let downCoordinate = x + 1
if (
downCoordinate <= MAX_ARR_INDEX &&
board[downCoordinate][y] == carId
) {
// can car move up?
let upCoordinate = x - 1
if (upCoordinate >= 0 && board[upCoordinate][y] == 0) {
steps.push({ [carId]: 'up' })
}
// can car move down?
let moveDownCoordinate = downCoordinate + 1
// 3 coordinate car
if (
moveDownCoordinate <= MAX_ARR_INDEX &&
board[moveDownCoordinate][y] == carId
) {
moveDownCoordinate++
}
if (
moveDownCoordinate <= MAX_ARR_INDEX &&
board[moveDownCoordinate][y] == 0
) {
steps.push({ [carId]: 'down' })
}
}
})
})
return steps
}
function applyStep(board, step) {
const carId = Object.keys(step)[0]
const value = Object.values(step)[0]
const newBoard = structuredClone(board)
if (value == 'up' || value == 'left') {
loop1: for (let x = 0; x <= MAX_ARR_INDEX; x++) {
loop2: for (let y = 0; y <= MAX_ARR_INDEX; y++) {
if (newBoard[x][y] == carId) {
if (value == 'up') {
newBoard[x - 1][y] = Number(carId)
if (newBoard[x + 2] && newBoard[x + 2][y] == carId) {
newBoard[x + 2][y] = 0
} else {
newBoard[x + 1][y] = 0
}
break loop1
} else if (value == 'left') {
newBoard[x][y - 1] = Number(carId)
if (newBoard[x][y + 2] && newBoard[x][y + 2] == carId) {
newBoard[x][y + 2] = 0
} else {
newBoard[x][y + 1] = 0
}
break loop1
}
}
}
}
} else {
loop1: for (let x = 5; x >= 0; x--) {
loop2: for (let y = 5; y >= 0; y--) {
if (newBoard[x][y] == carId) {
if (value == 'down') {
newBoard[x + 1][y] = Number(carId)
if (newBoard[x - 2] && newBoard[x - 2][y] == carId) {
newBoard[x - 2][y] = 0
} else {
newBoard[x - 1][y] = 0
}
break loop1
} else if (value == 'right') {
newBoard[x][y + 1] = Number(carId)
if (newBoard[x][y - 2] && newBoard[x][y - 2] == carId) {
newBoard[x][y - 2] = 0
} else {
newBoard[x][y - 1] = 0
}
break loop1
}
}
}
}
}
let carId_numberCoord = {}
newBoard.forEach((row, x) => {
row.forEach((carId, y) => {
const carIdTemp = newBoard[x][y]
if (carIdTemp != 0) {
if (carId_numberCoord[carIdTemp]) {
carId_numberCoord[carIdTemp] = carId_numberCoord[carIdTemp] += 1
} else {
carId_numberCoord[carIdTemp] = 1
}
}
})
})
// console.log(carId_numberCoord)
if (Object.values(carId_numberCoord).find((obj) => obj === 1)) {
console.log('orphan! before')
// prettyPrintBoard(board)
// console.log(step)
// prettyPrintBoard(newBoard)
throw new Error()
}
return newBoard
}
function solve(initial_board) {
let visited_board = {} // key:board value:any
let queued_board = [{ [JSON.stringify(initial_board)]: [] }] // [{board:steps}]
let count = 1
while (queued_board.length) {
let keyValue = queued_board.shift()
const current_board = JSON.parse(Object.keys(keyValue)[0])
const historical_steps = Object.values(keyValue)[0]
if (isGoal(current_board)) {
console.log(
'number of visited_board',
Object.keys(visited_board).length,
)
console.log('goal found', count)
console.log(historical_steps)
return
}
if (visited_board[JSON.stringify(current_board)]) continue
visited_board[JSON.stringify(current_board)] = true
let possibleSteps = getPossibleNextSteps(current_board)
possibleSteps.forEach((step) => {
const next_board = applyStep(current_board, step)
if (!visited_board[JSON.stringify(next_board)]) {
const next_steps = structuredClone(historical_steps)
next_steps.push(step)
queued_board.push({ [JSON.stringify(next_board)]: next_steps })
count++
}
})
}
}
solve(initial_board)