Merge branch 'master' of github.com:GeoffreyFrogeye/hashcode2016

This commit is contained in:
JLo'w 2016-02-11 21:43:42 +01:00
commit 12b8943514
2 changed files with 65 additions and 25 deletions

View file

@ -1,4 +1,13 @@
../submission.zip:
zip $@ *
all: 1o 2o 3o ../submission.zip
.PHONY: ../submission.zip
../submission.zip:
zip $@ .git *.py -r
1o: 1
python main.py $<
2o: 2
python main.py $<
3o: 3
python main.py $<
.PHONY: ../submission.zip 1o 2o 3o

75
main.py
View file

@ -14,7 +14,7 @@ P = 0 # Nb products
W = 0 # Nb warehouses
C = 0 # Nb customers
t = -1 # Turn
t = 0 # Turn
Dp = [] # Positions of drones
# (x, y)
@ -23,7 +23,7 @@ Di = [] # Items of drones
Dd = [] # Turn avaibility of drone
# int
Da = [] # Avaibles drones
# int
# bool
Pw = [] # Weight of products
# int
@ -87,7 +87,7 @@ for i in range(0, C):
for k in range(0, P):
orderQ[k] = 0
for j in range(0, nbP):
orderQ[int(info[j])] += 1
orderQ[int(info[j])] += -1
Ci.append(orderQ)
f.close()
@ -97,12 +97,13 @@ for d in range(D):
Dp.append(Wp[0])
Di.append(dict((p, 0) for p in range(P)))
Dd.append(0)
Da.append(True)
Out = [] # Drones commands
# Debug
assert(len(Dp) == len(Di) == len(Dd) == D)
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)
@ -123,20 +124,21 @@ def showGrid():
# Utilities
def distance(A, B):
return math.ceil(sqrt(pow((B[0] - A[0], 2) + pow(B[1] - A[1], 2))))
return math.ceil(math.sqrt(pow(B[0] - A[0], 2) + pow(B[1] - A[1], 2)))
def weight(d):
return sum([Pw[i] for i in Di[d]])
return sum(Di[d][p]*Pw[p]for p in range(P))
# Actions
def load(d, w, p, q):
# drone number, warehouse, product, qt
assert(d in Da)
assert(Da[d])
if (Dp[d] != Wp[w]):
Dd[d] += distance(Dp[d], Wp[w])
Dd[d] = Cw[c]
Wi[w][p] += -q
Di[d][p] += +q
assert(Wp[w][p] >= 0)
assert(Wi[w][p] >= 0)
assert(weight(d) <= M)
assert(Dd[d] <= T)
print("Drone", d, "loads", q, "of", p, "from warehouse", w, "", Dd[d])
@ -144,9 +146,10 @@ def load(d, w, p, q):
def unload(d, w, p, q):
# drone number, warehouse, product, qt
assert(d in Da)
assert(Da[d])
if (Dp[d] != Wp[w]):
Dd[d] += distance(Dp[d], Wp[w])
Dd[d] = Cw[c]
Wi[w][p] += +q
Di[d][p] += -q
assert(Dd[d] <= T)
@ -155,20 +158,22 @@ def unload(d, w, p, q):
def deliver(d, c, p, q):
# drone number, customer, product, qt
assert(d in Da)
assert(Da[d])
if (Dp[d] != Cp[c]):
Dd[d] += distance(Dp[d], Cp[c])
Ci[w][p] += +q
Dd[d] = Cp[c]
Ci[c][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])
print("Drone", d, "delivers", q, "of", p, "to client", c, "", Dd[d])
Out.append(str(d) + ' D ' + str(c) + ' ' + str(p) + ' ' + str(q))
def wait(d, w=1):
assert(d in Da)
assert(Da[d])
global Dd, Da
Dd[d] += w
Da.remove(d)
Da[d] = False
print("Drone", d, "waits", w, "turn" + ('s' if w >= 2 else ''), "", Dd[d])
Out.append(str(d) + ' W ' + str(w))
@ -176,26 +181,52 @@ def wait(d, w=1):
# Control
def newTurn():
global t
# Finishing turn
for d in [d for d in range(len(Da)) if Da[d]]:
print(d)
wait(d)
assert(sum(Da) == 0)
# New turn
t += 1
print("--- Turn", t)
for d in Da:
wait(d)
assert(len(Da) == 0)
for d in range(D):
if Dd[d] <= t:
Da.append(d)
print("Drones", ", ".join([str(d) for d in Da]), "(", len(Da), ")", "are avaible")
Da[d] = True
print("Drones", ", ".join([str(d) for d in range(len(Da)) if Da[d]]), "(", len(Da), ")", "are avaible")
def end():
print("--- End!")
# IA
newTurn()
newTurn()
def nearestW(p, pos):
minDist = math.inf
selW = None
for w in range(W):
if Wi[w][p] > 0:
dist = distance(Wp[w], pos)
if dist < minDist:
minDist = dist
selW = w
return selW
def listNeeds():
N = []
# client, product
for c in range(C):
for p in range(P):
for cp in range(abs(Ci[c][p])):
N.append((c, p))
return N
while t < 10:
N = listNeeds()
newTurn()
# Output
f = open(sys.argv[1] + 'o', 'w')
f.write(str(len(Out)) + '\n' + '\n'.join(Out))
f.write(str(len(Out)) + '\n' + '\n'.join(Out) + '\n')
f.close()
def SortCustomer():