From 8728346ace1c5e6f02fbfcc2732b26c6d1c3b155 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sat, 13 Feb 2016 13:09:14 +0100 Subject: [PATCH] Find nearest --- reborn.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/reborn.py b/reborn.py index b1aa999..4b57c36 100755 --- a/reborn.py +++ b/reborn.py @@ -44,6 +44,12 @@ class Warehouse: self.pos = pos self.items = items + def near(pos): + couples = [] + for el in __class__.ALL: + couples.append([el, distance(el.pos, pos)]) + return [couple[0] for couple in sorted(couples, key=lambda c: c[1])] + def get(pid): return __class__.ALL[pid] @@ -64,6 +70,12 @@ class Client: def satisfied(self): return len(self.needs) == 0 + def near(pos): + couples = [] + for el in __class__.ALL: + couples.append([el, distance(el.pos, pos)]) + return [couple[0] for couple in sorted(couples, key=lambda c: c[1])] + def get(pid): return __class__.ALL[pid] @@ -144,6 +156,12 @@ class Drone: log("Drone", self.id, "waits", turns, "turn" + ('s' if turns >= 2 else ''), "→", self.avail) output(self.id, 'W', turns) + def near(pos): + couples = [] + for el in __class__.ALL: + couples.append([el, distance(el.pos, pos)]) + return [couple[0] for couple in sorted(couples, key=lambda c: c[1])] + def get(pid): return __class__.ALL[pid] @@ -215,14 +233,15 @@ def newTurn(): try: d = 0 - N = Client.get(0).needs.copy() + c = Client.near(Warehouse.get(0).pos)[0] + N = c.needs.copy() for n in N: Drone.get(d).load(Warehouse.get(0), Product.get(n), 1) d += 1 newTurn() d = 0 for n in N: - Drone.get(d).deliver(Client.get(0), Product.get(Drone.get(d).items[0]), 1) + Drone.get(d).deliver(c, Product.get(Drone.get(d).items[0]), 1) d += 1