@@ -110,7 +110,6 @@ struct Grid {
110
110
}
111
111
112
112
private func inBounds( u: Index ) -> Bool {
113
- // assert(validMoves.contains(u.moves))
114
113
u. xy. x >= 0 && u. xy. x <= maxIndex. xy. x &&
115
114
u. xy. y >= 0 && u. xy. y <= maxIndex. xy. y &&
116
115
validMoves. contains ( u. moves)
@@ -134,28 +133,13 @@ struct Grid {
134
133
let hl = h. rotatedLeft ( )
135
134
let hr = h. rotatedRight ( )
136
135
137
- let start = 1
138
- let end = validMoves. upperBound
139
- let inSameDirection : [ Grid . Index ] =
140
- if start <= end {
141
- ( start... end) . map {
142
- Index ( xy: u. xy + $0 * h, heading: h, moves: u. moves + $0)
143
- }
144
- } else { [ ] }
145
-
146
- let turnStart = 1
147
- let turnEnd = validMoves. upperBound
148
- let afterTurning : [ Grid . Index ] =
149
- if turnStart <= turnEnd {
150
- ( turnStart... turnEnd) . flatMap {
151
- [
152
- Index ( xy: u. xy + $0 * hl, heading: hl, moves: $0) ,
153
- Index ( xy: u. xy + $0 * hr, heading: hr, moves: $0) ,
154
- ]
155
- }
156
- } else { [ ] }
157
-
158
- return ( inSameDirection + afterTurning)
136
+ return ( 1 ... max ( 2 , validMoves. upperBound) ) . flatMap {
137
+ [
138
+ Index ( xy: u. xy + $0 * h, heading: h, moves: u. moves + $0) ,
139
+ Index ( xy: u. xy + $0 * hl, heading: hl, moves: $0) ,
140
+ Index ( xy: u. xy + $0 * hr, heading: hr, moves: $0) ,
141
+ ]
142
+ }
159
143
}
160
144
161
145
/// Precondition: v must be adjacent to u
@@ -217,9 +201,6 @@ func shortestPath(grid: Grid, start: Grid.Index, visit: Visitor? = nil
217
201
// data structures. For real programs, consider using a priority queue, like
218
202
// the Heap in the Swift Collections package.
219
203
func popNearest( ) -> Grid . Index ? {
220
- // nextNearestD = Int.max
221
- // if let nn = nextNearest, !visited.contains(nn) { return nn }
222
-
223
204
while let md = inverseDistance. keys. min ( ) , let vs = inverseDistance [ md] {
224
205
for v in vs {
225
206
if pending. contains ( v) {
@@ -230,16 +211,6 @@ func shortestPath(grid: Grid, start: Grid.Index, visit: Visitor? = nil
230
211
inverseDistance [ md] = nil
231
212
}
232
213
return nil
233
- // var u: Grid.Index?
234
- // var ud = Int.max
235
- // for v in pending {
236
- // if let vd = distance[v], vd < ud {
237
- // u = v
238
- // ud = vd
239
- // }
240
- // }
241
- // if let u { pending.remove(u) }
242
- // return u
243
214
}
244
215
245
216
while let u = popNearest ( ) {
@@ -346,33 +317,29 @@ extension Grid {
346
317
}
347
318
348
319
func findShortestPath( grid: Grid , verbose: Bool ) -> Int ? {
349
- func sp( heading: ComplexInt ) -> Int ? {
350
- let startXY = ComplexInt ( x: 0 , y: 0 )
351
- let endXY = grid. maxIndex. xy;
352
-
353
- let start = Grid . Index ( xy: startXY, heading: heading, moves: 0 )
354
-
355
- let state = shortestPath ( grid: grid, start: start, visit: verbose ? trace : nil )
356
-
357
- // Find the minimum from amongst the distances of the original item.
358
- let endIndices = grid. expandedIndex ( xy: endXY)
359
- var end : Grid . Index ?
360
- var endDistance : Int ?
361
- for u in endIndices {
362
- if let d = state. distance [ u] , d < ( endDistance ?? Int . max) {
363
- end = u
364
- endDistance = d
365
- }
366
- }
367
- if verbose, let end = end {
368
- print (
369
- grid. renderToString ( state: state, start: start, ends: Set ( [ end] ) ) ,
370
- terminator: " " )
320
+ let startXY = ComplexInt ( x: 0 , y: 0 )
321
+ let endXY = grid. maxIndex. xy;
322
+
323
+ let start = Grid . Index ( xy: startXY, heading: . east, moves: 0 )
324
+
325
+ let state = shortestPath ( grid: grid, start: start, visit: verbose ? trace : nil )
326
+
327
+ // Find the minimum from amongst the distances of the original item.
328
+ let endIndices = grid. expandedIndex ( xy: endXY)
329
+ var end : Grid . Index ?
330
+ var endDistance : Int ?
331
+ for u in endIndices {
332
+ if let d = state. distance [ u] , d < ( endDistance ?? Int . max) {
333
+ end = u
334
+ endDistance = d
371
335
}
372
- return endDistance
373
336
}
374
-
375
- return sp ( heading: . east)
337
+ if verbose, let end = end {
338
+ print (
339
+ grid. renderToString ( state: state, start: start, ends: Set ( [ end] ) ) ,
340
+ terminator: " " )
341
+ }
342
+ return endDistance
376
343
}
377
344
378
345
/// Reuse the function that shows the state of the grid after shortest path has
0 commit comments