# 8 python neighbor counting puzzle

I am writing an algorithm for solving 8 puzzles. https://en.wikipedia.org/wiki/15_puzzle

The state is represented by the tuple (1,2,3,4,5,6,7,8,0), where 0 is an empty box (this is equivalent to a 3 * 3 matrix).

Given the state of the puzzle p = (1,3,2,5,4,6,0,7,8), I wrote a function to calculate the neighbors.

``````def neighbours(p):
p = np.array(p).reshape(3,3)
ind0 = np.argwhere(p == 0).squeeze()
x0, y0 = ind0

ind_neig = [[ind0 - 1, ind0],
[ind0 + 1, ind0],
[ind0, ind0 - 1],
[ind0, ind0 + 1]]

ind_neig = [ind for ind in ind_neig if min(ind) >= 0 and max(ind) < 3]

neig = [ p.copy() for i in range(len(ind_neig))]

for i in range(len(ind_neig)):
x, y = ind_neig[i]
neig[i][x0, y0] = p[x, y]
neig[i][x, y] = 0

neig = [tuple(np.ravel(i)) for i in neig]
return neig
```

```

I need a faster version of the neighbors function and may not need the numpy library. In particular, I need a function that can be used also when the puzzle is larger, such as a 15-puzzle

+3

source to share

I think you will find the following implementation pretty straightforward, plus it doesn't use numpy:

``````def neighbours(p):
res = []
ind = p.index(0)
if not top(ind):
res.append(up_neig(p, ind))
if not bottom(ind):
res.append(down_neig(p, ind))
if not left(ind):
res.append(left_neig(p, ind))
if not right(ind):
res.append(right_neig(p, ind))
return res

def top(ind):
return ind < 3

def bottom(ind):
return ind > 5

def left(ind):
return ind in [0, 3, 6]

def right(ind):
return ind in [2, 5, 8]

def up_neig(p, ind):
_p = list(p)
_p[ind], _p[ind-3] = _p[ind-3], _p[ind]
return tuple(_p)

def down_neig(p, ind):
_p = list(p)
_p[ind], _p[ind+3] = _p[ind+3], _p[ind]
return tuple(_p)

def left_neig(p, ind):
_p = list(p)
_p[ind], _p[ind-1] = _p[ind-1], _p[ind]
return tuple(_p)

def right_neig(p, ind):
_p = list(p)
_p[ind], _p[ind+1] = _p[ind+1], _p[ind]
return tuple(_p)

p = (1,3,2,5,4,6,0,7,8)
print neighbours(p)  # [(1, 3, 2, 0, 4, 6, 5, 7, 8), (1, 3, 2, 5, 4, 6, 7, 0, 8)]
```

```
+2

source

All Articles