@@ -41,6 +41,12 @@ class @Grid
4141 grid[sourceRow][sourceCol] = 0
4242
4343 nextGrid = new Grid (grid, emptyPos)
44+
45+ number = grid[targetRow][targetCol]
46+ nextGrid ._lowerSolutionBound = @ lowerSolutionBound () -
47+ rectilinearDistance (number, sourceRow, sourceCol) +
48+ rectilinearDistance (number, targetRow, targetCol)
49+
4450 return nextGrid
4551
4652 applyMoves : (sourceDirections ) ->
@@ -59,20 +65,24 @@ class @Grid
5965 from where each number is to where it should
6066 be
6167 ###
62- moveCount = 0
68+ if not @_lowerSolutionBound ?
69+
70+ moveCount = 0
71+
72+ for rowNum of @grid
73+ rowNum = parseInt (rowNum, 10 )
74+ for colNum of @grid [rowNum]
75+ colNum = parseInt (colNum, 10 )
6376
64- for rowNum of @grid
65- rowNum = parseInt (rowNum, 10 )
66- for colNum of @grid [rowNum]
67- colNum = parseInt (colNum, 10 )
77+ number = @grid [rowNum][colNum]
6878
69- number = @grid [rowNum][colNum]
79+ continue if number == 0
7080
71- continue if number == 0
81+ moveCount += rectilinearDistance number, rowNum, colNum
7282
73- moveCount += rectilinearDistance number, rowNum, colNum
83+ @_lowerSolutionBound = moveCount
7484
75- return moveCount
85+ return @_lowerSolutionBound
7686
7787 log : ->
7888 console .log " Empty: #{ @emptyPos } "
0 commit comments