Skip to content

Instantly share code, notes, and snippets.

@methane
Created September 3, 2011 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save methane/1191720 to your computer and use it in GitHub Desktop.
Save methane/1191720 to your computer and use it in GitHub Desktop.
def check_route(board, route):
w = board.w
h = board.h
state = bytearray(board.state)
pos = board.state.index(b'0')
try:
for c in route:
if c == 'D':
npos = pos+w
if npos >= w*h:
return False
elif c == 'U':
npos = pos-w
if npos < 0:
return False
elif c == 'L':
if pos%w == 0:
return False
npos = pos-1
elif c == 'R':
if (pos+1)%w == 0:
return False
npos = pos+1
else:
return False
if state[npos] == b'=' or state[pos] == b'=':
return False
state[pos], state[npos] = state[npos], state[pos]
pos = npos
except IndexError:
return False
expected = make_goal(board.state)
result = bytes(state)
return expected == result
PLATES = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0'
def make_goal(state):
N = len(state)
state = bytearray(state)
for i in xrange(N):
if state[i] != ord('='):
state[i] = PLATES[i]
state[-1] = '0'
return bytes(state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment