Skip to content

Commit fc50d5e

Browse files
committed
Cleanup 17
1 parent 07c96a1 commit fc50d5e

6 files changed

+24
-1355
lines changed

17.p2-expand-opt.swift renamed to 17.expand.swift

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ func shortestPath(grid: Grid, start: Grid.Index, visit: Visitor? = nil
266266
}
267267

268268
func trace(state: DijkstraState) {
269-
// if state.iteration % 500 == 0 {
270-
// let total = state.grid.totalItems
271-
// print("iteration \(state.iteration) found tentative distances to \(state.distance.count) / \(total) items")
272-
// }
269+
if state.iteration % 500 == 0 {
270+
let total = state.grid.totalItems
271+
print("iteration \(state.iteration) found tentative distances to \(state.distance.count) / \(total) items")
272+
}
273273
}
274274

275275
extension Grid {
@@ -345,14 +345,14 @@ extension Grid {
345345
}
346346
}
347347

348-
func ourShortestPath(grid: Grid) -> Int? {
348+
func findShortestPath(grid: Grid, verbose: Bool) -> Int? {
349349
func sp(heading: ComplexInt) -> Int? {
350350
let startXY = ComplexInt(x: 0, y: 0)
351351
let endXY = grid.maxIndex.xy;
352352

353353
let start = Grid.Index(xy: startXY, heading: heading, moves: 0)
354354

355-
let state = shortestPath(grid: grid, start: start, visit: trace)
355+
let state = shortestPath(grid: grid, start: start, visit: verbose ? trace : nil)
356356

357357
// Find the minimum from amongst the distances of the original item.
358358
let endIndices = grid.expandedIndex(xy: endXY)
@@ -364,11 +364,10 @@ func ourShortestPath(grid: Grid) -> Int? {
364364
endDistance = d
365365
}
366366
}
367-
if let end {
368-
_ = end
369-
// print(
370-
// grid.renderToString(state: state, start: start, ends: Set([end])),
371-
// terminator: "")
367+
if verbose, let end = end {
368+
print(
369+
grid.renderToString(state: state, start: start, ends: Set([end])),
370+
terminator: "")
372371
}
373372
return endDistance
374373
}
@@ -409,11 +408,20 @@ let validMovesP1 = 0...3
409408
/// a maximum of 10 blocks before we must turn.
410409
let validMovesP2 = 4...10
411410

411+
let showNeighbours = CommandLine.arguments.last == "-n"
412+
let verbose = CommandLine.arguments.last == "-v" || showNeighbours
413+
let input = readInput()
414+
412415
/// A driver function
413-
func sp(_ input: [[Int]], _ validMoves: ClosedRange<Int>) -> Int {
416+
func sp(_ validMoves: ClosedRange<Int>) -> Int {
414417
let grid = Grid(items: input, validMoves: validMoves)
415-
return ourShortestPath(grid: grid) ?? -1
418+
return findShortestPath(grid: grid, verbose: verbose) ?? -1
416419
}
417420

418-
let input = readInput()
419-
print(sp(input, validMovesP1), sp(input, validMovesP2))
421+
if (showNeighbours) {
422+
let grid = Grid(items: input, validMoves: validMovesP1)
423+
let u = Grid.Index(xy: .init(x: 0, y: 0), heading: .east, moves: 0)
424+
printNeighbours(u, grid: grid)
425+
} else {
426+
print(sp(validMovesP1), sp(validMovesP2))
427+
}

17.graph.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func shortestPath<T>(
150150
let w = grid.edgeWeight(from: u, to: v)
151151
if dv > du + w {
152152
distance[v] = du + w
153+
parent[v] = u
153154
}
154155
pending.insert(v)
155156
}

0 commit comments

Comments
 (0)