Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Created May 24, 2018 19:51
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 sbsatter/eeaa50d1b892135166bc70c338204b19 to your computer and use it in GitHub Desktop.
Save sbsatter/eeaa50d1b892135166bc70c338204b19 to your computer and use it in GitHub Desktop.
Bot Saves Princess Hackerrank AI problem: https://www.hackerrank.com/challenges/saveprincess
#!/usr/bin/python
import math
def displayPathtoPrincess(n,grid):
#print all the moves here
princess = ()
for row in range(n):
for col in range(len(grid[row])):
if grid[row][col] == 'p':
princess = (row, col)
break
m = (n//2, n//2)
x = m[0] - princess[0]
y = m[1] - princess[1]
dir = 'UP\n' if x > 0 else 'DOWN\n'
print(dir * int(math.fabs(x)), end='')
dir = 'LEFT\n' if y > 0 else 'RIGHT\n'
print(dir * int(math.fabs(y)), end='')
m = int(input())
grid = []
for i in range(0, m):
grid.append(input().strip())
displayPathtoPrincess(m,grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment