Created
September 3, 2011 20:19
-
-
Save methane/1191720 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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