script.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. '''
  2. @author: olivier.massot, 2019
  3. '''
  4. import heapq
  5. import sys
  6. DEBUG = True
  7. def log(x):
  8. if DEBUG:
  9. print(x, file=sys.stderr)
  10. # Cells
  11. START_0 = '0'
  12. START_1 = '1'
  13. BLUEBERRIES_CRATE = "B"
  14. ICE_CREAM_CRATE = "I"
  15. STRAWBERRIES_CRATE = "S"
  16. CHOPPING_BOARD = "C"
  17. DOUGH_CRATE = "H"
  18. WINDOW = "W"
  19. EMPTY_TABLE = "#"
  20. DISHWASHER = "D"
  21. FLOOR_CELL = "."
  22. OVEN = "O"
  23. # Items
  24. NONE = "NONE"
  25. DISH = "DISH"
  26. ICE_CREAM = "ICE_CREAM"
  27. BLUEBERRIES = "BLUEBERRIES"
  28. STRAWBERRIES = "STRAWBERRIES"
  29. CHOPPED_STRAWBERRIES = "CHOPPED_STRAWBERRIES"
  30. DOUGH = "DOUGH"
  31. CROISSANT = "CROISSANT"
  32. class Base():
  33. def __repr__(self):
  34. return f"<{self.__class__.__name__}: {self.__dict__}>"
  35. class Customer(Base):
  36. def __init__(self, item, award):
  37. self.item = item
  38. self.award = int(award)
  39. class Cook(Base):
  40. def __init__(self, x, y, item):
  41. self.x = int(x)
  42. self.y = int(y)
  43. self.item = item
  44. self.order = []
  45. @property
  46. def pos(self):
  47. return (self.x, self.y)
  48. def update(self, x, y, item):
  49. self.x = int(x)
  50. self.y = int(y)
  51. self.item = item
  52. def use(self, x, y, msg=""):
  53. print("USE", x, y, msg)
  54. def move(self, x, y):
  55. print("MOVE", x, y)
  56. def wait(self):
  57. print("WAIT")
  58. class Table(Base):
  59. def __init__(self, x, y, item):
  60. self.x = int(x)
  61. self.y = int(y)
  62. self.item = item
  63. class Oven(Base):
  64. def __init__(self, content, timer):
  65. self.content = content
  66. self.timer = int(timer)
  67. class PathNode(tuple):
  68. def __new__(self, x, y, parent=None):
  69. n = tuple.__new__(self, (x, y))
  70. n.parent = parent
  71. n.cost = 0
  72. return n
  73. class Grid(Base):
  74. def __init__(self, cells):
  75. self.cells = cells
  76. self.w, self.h = len(cells[0]), len(cells)
  77. self.add_costs = {}
  78. def at(self, x, y):
  79. return self.cells[y][x]
  80. def flatten(self):
  81. return [(x, y, c) for y, row in enumerate(self.cells) for x, c in enumerate(row)]
  82. @property
  83. def coords(self):
  84. return [(x, y) for y in range(self.h) for x in range(self.w)]
  85. def where_are(self, content):
  86. return [(x, y) for x, y, c in self.flatten() if c == content]
  87. @staticmethod
  88. def distance(from_, to_):
  89. return abs(from_[0] - to_[0]) + abs(from_[1] - to_[1])
  90. def closest(self, from_, content):
  91. return sorted([(c, Grid.distance(from_, c)) for c in self.where_are(content)], key=lambda k: k[1])[0]
  92. def neighbors(self, x, y, diags=True):
  93. neighs = [(x, y - 1), (x - 1, y), (x + 1, y), (x, y + 1)]
  94. if diags:
  95. neighs += [(x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1), (x + 1, y + 1)]
  96. return [(x, y) for x, y in neighs if 0 <= x < self.w and 0 <= y < self.h]
  97. def passable(self, x, y):
  98. return self.at(x, y) in (FLOOR_CELL, START_0, START_1)
  99. def cost(self, x, y):
  100. return 10 + self.add_costs.get((x, y), 0)
  101. def path(self, origin, target, incl_start=False):
  102. nodes = []
  103. origin = PathNode(*origin)
  104. targets = grid.neighbors(*target)
  105. heapq.heappush(nodes, (0, origin))
  106. while nodes:
  107. current = heapq.heappop(nodes)[1]
  108. if current in targets:
  109. path = []
  110. next_ = current
  111. while next_:
  112. if next_ != origin or incl_start:
  113. path.insert(0, next_)
  114. next_ = next_.parent
  115. return path
  116. neighbors = self.neighbors(*current, False)
  117. for x, y in neighbors:
  118. if not self.passable(x, y):
  119. continue
  120. cost = current.cost + self.cost(x, y)
  121. priority = cost + 10 * (abs(x - target[0]) + abs(y - target[1]))
  122. node = PathNode(x, y, current)
  123. node.cost = cost
  124. heapq.heappush(nodes, (priority, node))
  125. else:
  126. return None
  127. # input vars
  128. num_all_customers = int(input())
  129. all_customers = [Customer(*input().split()) for _ in range(num_all_customers)]
  130. grid = Grid([list(input()) for i in range(7)])
  131. log(f"{num_all_customers} customers: {all_customers}")
  132. log(f"grid: {grid}")
  133. player = Cook(-1, -1, "")
  134. partner = Cook(-1, -1, "")
  135. oven = Oven(NONE, 0)
  136. location = {DISH: DISHWASHER,
  137. ICE_CREAM: ICE_CREAM_CRATE,
  138. BLUEBERRIES: BLUEBERRIES_CRATE,
  139. STRAWBERRIES: STRAWBERRIES_CRATE,
  140. CHOPPED_STRAWBERRIES: CHOPPING_BOARD,
  141. CROISSANT: OVEN,
  142. DOUGH: DOUGH_CRATE,
  143. OVEN: OVEN,
  144. WINDOW: WINDOW}
  145. special = list(location.values())
  146. class Task(Base):
  147. def __init__(self, name, from_):
  148. self.name = name
  149. self.loc = location[name]
  150. self.pos, self.dist = grid.closest(from_, self.loc)
  151. ingredients_for = {CHOPPED_STRAWBERRIES: STRAWBERRIES,
  152. CROISSANT: DOUGH}
  153. needs_ingredients = list(ingredients_for.keys())
  154. preparation_for = {STRAWBERRIES: CHOPPED_STRAWBERRIES,
  155. DOUGH: CROISSANT}
  156. needs_preparation = list(preparation_for.keys())
  157. oven_time = {CROISSANT: 10}
  158. needs_oven = list(oven_time.keys())
  159. def whats_next(todo):
  160. if not todo:
  161. # no task left, target is the window
  162. return Task(WINDOW, player.pos)
  163. # special case: one or more ingredients needs preparation
  164. if any((t for t in todo if t.name in needs_ingredients)):
  165. # If cook has an ingredient in hands, he needs to prepare it
  166. if player.item in needs_preparation:
  167. return Task(preparation_for[player.item], player.pos)
  168. # If hands are free and an ingredient is needed, we go for it first except if it is already in the oven
  169. if player.item == NONE and any((t for t in todo if t.name in needs_ingredients and not t.name in oven.content)):
  170. return Task(next((ingredients_for[t.name] for t in todo if t.name in needs_ingredients)), player.pos)
  171. #
  172. #
  173. # # special case, one or more plates needs to be cooked
  174. # if any((t for t in todo if t.name in needs_oven)):
  175. #
  176. # # if oven has ended, go grab the oven's content
  177. # if oven.timer <=1 and any((t for t in todo if t.name in oven.content)):
  178. # return Task(OVEN, player.pos)
  179. #
  180. # # if oven is empty, go fill it
  181. # if oven.content == NONE:
  182. # return Task(next((ingredients_for[t.name] for t in todo if t.name in needs_oven)), player.pos)
  183. if player.item != NONE and not DISH in player.item:
  184. # cook has something in its hands and no dish, he have to take one
  185. return next((t for t in todo if t.name == DISH))
  186. # else, go for the closest task
  187. tasks = sorted(todo, key= lambda x: x.dist)
  188. return next(iter(tasks))
  189. while True:
  190. turns_remaining = int(input())
  191. player.update(*input().split())
  192. log(f"*** player: {player}")
  193. partner.update(*input().split())
  194. log(f"*** partner: {partner}")
  195. num_tables_with_items = int(input()) # the number of tables in the kitchen that currently hold an item
  196. tables = [Table(*input().split()) for _ in range(num_tables_with_items)]
  197. log(f"*** tables: {tables}")
  198. oven = Oven(*input().split())
  199. log(f"*** oven: {oven}")
  200. num_customers = int(input()) # the number of customers currently waiting for food
  201. customers = [Customer(*input().split()) for _ in range(num_customers)]
  202. log(f"*** customers: {customers}")
  203. ### SCRIPT
  204. # if no current order, take the most beneficial
  205. if not player.order:
  206. queue = sorted(customers, reverse=True, key=lambda x: x.award)
  207. player.order = queue[0].item.split('-')
  208. log(f'>>> new order taken: {player.order}')
  209. todo = [Task(t, player.pos) for t in player.order if t not in player.item]
  210. log(f"todo: {todo}")
  211. next_task = whats_next(todo)
  212. log(f"next_task: {next_task}")
  213. if next_task.name == WINDOW:
  214. player.order = []
  215. # update grid movement costs following the probability of finding the partner here
  216. partner_could_be_there = [(x, y) for x, y in grid.coords if grid.passable(x, y) and grid.distance(partner.pos, (x, y)) <= 4]
  217. grid.add_costs = {}
  218. for x, y in partner_could_be_there:
  219. k1 = 2 if (x, y) == partner.pos else 1
  220. # cell is next to a special cell, partner has more chance to stop there
  221. k2 = 2 if any((c for c in grid.neighbors(x, y) if grid.at(*c) in special )) else 1
  222. grid.add_costs[(x, y)] = k1 * k2 * 3
  223. log(grid.add_costs)
  224. path = grid.path(player.pos, next_task.pos)
  225. log(path)
  226. if path is not None:
  227. if len(path) > 0:
  228. if len(path) > 4:
  229. player.move(*path[3])
  230. else:
  231. player.move(*path[-1])
  232. else:
  233. player.use(*next_task.pos)
  234. else:
  235. player.use(*next_task.pos)