@@ -266,10 +266,10 @@ func shortestPath(grid: Grid, start: Grid.Index, visit: Visitor? = nil
266
266
}
267
267
268
268
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
+ }
273
273
}
274
274
275
275
extension Grid {
@@ -345,14 +345,14 @@ extension Grid {
345
345
}
346
346
}
347
347
348
- func ourShortestPath ( grid: Grid ) -> Int ? {
348
+ func findShortestPath ( grid: Grid , verbose : Bool ) -> Int ? {
349
349
func sp( heading: ComplexInt ) -> Int ? {
350
350
let startXY = ComplexInt ( x: 0 , y: 0 )
351
351
let endXY = grid. maxIndex. xy;
352
352
353
353
let start = Grid . Index ( xy: startXY, heading: heading, moves: 0 )
354
354
355
- let state = shortestPath ( grid: grid, start: start, visit: trace)
355
+ let state = shortestPath ( grid: grid, start: start, visit: verbose ? trace : nil )
356
356
357
357
// Find the minimum from amongst the distances of the original item.
358
358
let endIndices = grid. expandedIndex ( xy: endXY)
@@ -364,11 +364,10 @@ func ourShortestPath(grid: Grid) -> Int? {
364
364
endDistance = d
365
365
}
366
366
}
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: " " )
372
371
}
373
372
return endDistance
374
373
}
@@ -409,11 +408,20 @@ let validMovesP1 = 0...3
409
408
/// a maximum of 10 blocks before we must turn.
410
409
let validMovesP2 = 4 ... 10
411
410
411
+ let showNeighbours = CommandLine . arguments. last == " -n "
412
+ let verbose = CommandLine . arguments. last == " -v " || showNeighbours
413
+ let input = readInput ( )
414
+
412
415
/// A driver function
413
- func sp( _ input : [ [ Int ] ] , _ validMoves: ClosedRange < Int > ) -> Int {
416
+ func sp( _ validMoves: ClosedRange < Int > ) -> Int {
414
417
let grid = Grid ( items: input, validMoves: validMoves)
415
- return ourShortestPath ( grid: grid) ?? - 1
418
+ return findShortestPath ( grid: grid, verbose : verbose ) ?? - 1
416
419
}
417
420
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
+ }
0 commit comments