Created
June 24, 2016 16:23
-
-
Save thedangler/3b7b65d0029548fef795768dbdaba457 to your computer and use it in GitHub Desktop.
BotClean Partially Observable : hackerrank.com
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/python3 | |
import json | |
import os | |
def update_board(board): | |
if os.path.isfile("board"): | |
old_board = json.load(open("board")) | |
for i in range(len(board)): | |
for j in range(len(board[0])): | |
if board[i][j] == "o": | |
board[i][j] = old_board[i][j] | |
json.dump(board, open("board", "w")) | |
def next_move(posx, posy, board): | |
grid = board | |
bot = (posx,posy) | |
y = 0 | |
x = 0 | |
clean_cell = (0,0) | |
hidden_cell = (0,0) | |
min = None | |
hidden = None | |
CMD = None | |
found = False | |
if grid[bot[0]][bot[1]] == 'd': | |
CMD = 'CLEAN' | |
for row in grid: | |
x = 0 | |
for spot in row: | |
if spot == 'd': | |
found = True | |
d = abs(bot[1] - x)+abs(bot[0] - y) # closest cell | |
if min == None or d < min: | |
min = d | |
clean_cell = (y,x) | |
#find closest hiddent cell to goto | |
if spot == 'o': | |
o = abs(bot[1] - x)+abs(bot[0] - y) #closest cell | |
if hidden == None or o < hidden: | |
hidden = o | |
hidden_cell = (y,x) | |
x += 1 | |
y += 1 | |
if CMD is None: | |
# goto the hidden cell | |
if found == False: | |
if bot[0] < hidden_cell[0]: | |
CMD = 'DOWN' | |
elif bot[0] > hidden_cell[0]: | |
CMD = "UP" | |
elif bot[1] < hidden_cell[1]: | |
CMD = 'RIGHT' | |
else: | |
CMD = "LEFT" | |
else: # goto the dirty cell | |
if bot[0] < clean_cell[0]: | |
CMD = 'DOWN' | |
elif bot[0] > clean_cell[0]: | |
CMD = "UP" | |
elif bot[1] < clean_cell[1]: | |
CMD = 'RIGHT' | |
else: | |
CMD = "LEFT" | |
print(CMD) | |
if __name__ == "__main__": | |
pos = [int(i) for i in input().strip().split()] | |
board = [[j for j in input().strip()] for i in range(5)] | |
update_board(board) | |
next_move(pos[0], pos[1], board) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment