Skip to content

Commit 0df13ee

Browse files
committed
Touch up
1 parent fc50d5e commit 0df13ee

File tree

1 file changed

+28
-61
lines changed

1 file changed

+28
-61
lines changed

17.expand.swift

Lines changed: 28 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ struct Grid {
110110
}
111111

112112
private func inBounds(u: Index) -> Bool {
113-
// assert(validMoves.contains(u.moves))
114113
u.xy.x >= 0 && u.xy.x <= maxIndex.xy.x &&
115114
u.xy.y >= 0 && u.xy.y <= maxIndex.xy.y &&
116115
validMoves.contains(u.moves)
@@ -134,28 +133,13 @@ struct Grid {
134133
let hl = h.rotatedLeft()
135134
let hr = h.rotatedRight()
136135

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+
}
159143
}
160144

161145
/// Precondition: v must be adjacent to u
@@ -217,9 +201,6 @@ func shortestPath(grid: Grid, start: Grid.Index, visit: Visitor? = nil
217201
// data structures. For real programs, consider using a priority queue, like
218202
// the Heap in the Swift Collections package.
219203
func popNearest() -> Grid.Index? {
220-
// nextNearestD = Int.max
221-
// if let nn = nextNearest, !visited.contains(nn) { return nn }
222-
223204
while let md = inverseDistance.keys.min(), let vs = inverseDistance[md] {
224205
for v in vs {
225206
if pending.contains(v) {
@@ -230,16 +211,6 @@ func shortestPath(grid: Grid, start: Grid.Index, visit: Visitor? = nil
230211
inverseDistance[md] = nil
231212
}
232213
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
243214
}
244215

245216
while let u = popNearest() {
@@ -346,33 +317,29 @@ extension Grid {
346317
}
347318

348319
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
371335
}
372-
return endDistance
373336
}
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
376343
}
377344

378345
/// Reuse the function that shows the state of the grid after shortest path has

0 commit comments

Comments
 (0)