main.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import sys
  2. from random import randint
  3. from models.robot import Robot
  4. # boundary x, boundary y, count for generator expression
  5. def gen_coords(bx, by, c):
  6. for i in range(c):
  7. yield (randint(bx, by), randint(bx, by))
  8. def dist(ab, xy):
  9. return abs(ab[0] + xy[0]) + abs(ab[1] + xy[1])
  10. def main(argv):
  11. dust = [particle for particle in gen_coords(0, 9, 10)]
  12. print('dust particles', dust)
  13. robot = Robot(40)
  14. dust.sort(key=lambda d: d[0] + d[1])
  15. print('dust sorted ', dust)
  16. while len(dust) > 0:
  17. clo, ind = robot.closest(dust)
  18. print('next closest particle would be', clo, 'at index', ind)
  19. # if the way to the closest particle + the way back is possible with
  20. # the energy that is left
  21. way = robot.dist(clo)
  22. ori = dist(clo, (0, 0))
  23. if way + ori <= robot.energy():
  24. # drive to closest
  25. robot.drive(clo, way)
  26. print(clo, 'popped')
  27. dust.pop(ind)
  28. else:
  29. print(way, '+', ori, 'is too far away, robot has', robot.energy(), 'energy left')
  30. way = robot.dist((0, 0))
  31. robot.drive((0, 0), way)
  32. print(dust)
  33. if __name__ == "__main__":
  34. main(sys.argv)