Created
May 6, 2013 12:19
-
-
Save jamiees2/5524790 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
#!/usr/bin/python | |
# Head ends here | |
dirts = [] | |
unfound = [] | |
full_board = None | |
selected = None | |
def update_board(board): | |
global full_board, unfound, dirts, update_count | |
if full_board == None: | |
full_board = list(board) | |
for x in xrange(len(full_board)): | |
for y in xrange(len(full_board[x])): | |
if board[x][y] != 'o': | |
full_board[x][y] = board[x][y] | |
if full_board[x][y] == 'o' and not (x,y) in unfound: | |
unfound.append((x,y)) | |
elif full_board[x][y] == 'd' and not (x,y) in dirts: | |
dirts.append((x,y)) | |
def find_closest(x, y, arr): | |
min_dist = 10**2#something huge | |
for i in xrange(len(arr)): | |
arr_x, arr_y = arr[i] | |
dist_x = x - arr_x if x > arr_x else arr_x - x | |
dist_y = y - arr_y if y > arr_y else arr_y - y | |
dist = dist_x + dist_y | |
if dist < min_dist: | |
min_idx = i | |
min_dist = dist | |
return min_idx | |
# Head ends here | |
def next_move(x, y, board): | |
global selected | |
global dirts | |
global unfound | |
update_board(board) | |
if selected == None: | |
if len(dirts) == 0: | |
selected = (False,find_closest(x,y,unfound)) | |
else: | |
selected = (True,find_closest(x,y,dirts)) | |
in_dirts, point = selected | |
dirt_x, dirt_y = dirts[point] if in_dirts else unfound[point] | |
if not in_dirts and x == dirt_x and y == dirt_y: | |
unfound.pop(point) | |
selected = None | |
return next_move(x,y,board) | |
if in_dirts and x == dirt_x and y == dirt_y: | |
print "CLEAN" | |
dirts.pop(point) | |
selected = None | |
elif dirt_x > x: | |
print "DOWN" | |
elif x > dirt_x: | |
print "UP" | |
elif y > dirt_y: | |
print "LEFT" | |
elif dirt_y > y: | |
print "RIGHT" | |
# Tail starts here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment