This repository has been archived on 2019-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
hashcode2016/main.py

122 lines
2.6 KiB
Python
Raw Normal View History

2016-02-11 17:43:32 +00:00
#!/usr/bin/env python3
2016-02-11 18:18:29 +00:00
import math
2016-02-11 17:43:32 +00:00
print("#hashcode2016")
2016-02-11 18:18:29 +00:00
X = 0 # Nb rows
Y = 0 # Nb columns
D = 0 # Nb drones
T = 0 # Deadline
M = 0 # Maximum load
P = 0 # Nb products
W = 0 # Nb warehouses
C = 0 # Nb customers
2016-02-11 18:56:36 +00:00
t = 0 # Turn
2016-02-11 18:18:29 +00:00
Dp = []; # Positions of drones
# (x, y)
Di = []; # Items of drones
# {product number: qt}
Dd = []; # Turn avaibility of drone
# int
2016-02-11 18:56:36 +00:00
Da = []; # Avaibles drones
# int
2016-02-11 18:18:29 +00:00
Pw = []; # Weight of products
# int
Wp = []; # Positions of warehouses
# (x, y)
Wi = []; # Items of warehouses
# {product number: qt}
Cp = []; # Positions of customers
# (x, y)
Ci = []; # Needs of customers
# {product number: qt}
2016-02-11 19:10:18 +00:00
# Debug
assert(len(Dp) == len(Di) == len(Dd) == len(Da) == D)
assert(len(Pw) == P)
assert(len(Wp) == len(Wi) == W)
assert(len(Cp) == len(Ci) == C)
#def droneInfos(d):
# print("- Drone", d, "carries", ", ".join([p + ": " + q + "×" + Dw[p] for
def showGrid():
for y in range(Y):
for x in range(X):
if (x, y) in Wp:
print("W", end="")
if (x, y) in Cp:
print("C", end="")
else:
print("·", end="")
2016-02-11 18:56:36 +00:00
# Utilities
2016-02-11 18:18:29 +00:00
def distance(A, B):
return math.ceil(sqrt(pow((B[0] - A[0], 2) + pow(B[1] - A[1], 2))))
2016-02-11 18:56:36 +00:00
def weight(d):
return sum([Pw[i] for i in Di[d]])
# Actions
def load(d, w, p, q):
# drone number, warehouse, product, qt
assert(d in Da)
if (Dp[d] != Wp[w]):
Dd[d] += distance(Dp[d], Wp[w])
Wi[w][p] += -q
Di[d][p] += +q
assert(Wp[w][p] >= 0)
assert(weight(d) <= M)
assert(Dd[d] <= T);
print("Drone", d, "loads", q, "of", p, "from warehouse", w, "", Dd[d])
def unload(d, w, p, q):
# drone number, warehouse, product, qt
assert(d in Da)
if (Dp[d] != Wp[w]):
Dd[d] += distance(Dp[d], Wp[w])
Wi[w][p] += +q
Di[d][p] += -q
assert(Dd[d] <= T);
print("Drone", d, "unloads", q, "of", p, "from warehouse", w, "", Dd[d])
def deliver(d, c, p, q):
# drone number, customer, product, qt
assert(d in Da)
if (Dp[d] != Cp[c]):
Dd[d] += distance(Dp[d], Cp[c])
Ci[w][p] += +q
Di[d][p] += -q
Dd[d] += 1
assert(Dd[d] <= T);
print("Drone", d, "delivers", q, "of", p, "to client", w, "", Dd[d])
def wait(d, w=1):
assert(d in Da)
Dd[d] += w
print("Drone", d, "waits", w, "turn" + ('s' if w >= 2 else ''), "", Dd[d])
# Control
def newTurn():
t += 1
print("--- Turn", t)
for d in Da:
wait(d)
2016-02-11 19:10:18 +00:00
assert(len(Da) == 0)
2016-02-11 18:56:36 +00:00
for d in range(D):
if Dd[d] <= t:
Da.push(d)
print("Drones", ", ".join(Da), "(", len(Da), ")", "are avaible")
def end():
print("--- End!")
2016-02-11 18:18:29 +00:00
2016-02-11 18:56:36 +00:00
# IA