-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrone.java
More file actions
150 lines (133 loc) · 4.97 KB
/
Drone.java
File metadata and controls
150 lines (133 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package hashcode.model;
import hashcode.Main;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Drone {
public static Drone[] drones;
static public int MAX_WEIGHT;
public int id;
public Point position;
public int currentWarehouse;
public Map<Integer, Integer> products = new HashMap<>();
public Map<Integer, Integer> currentProducts = new HashMap<Integer, Integer>();
public int freeSpace = MAX_WEIGHT;
public int TTL = Main.TURNS;
public Drone(int id, Point point) {
this.id = id;
this.position = point;
}
public boolean addProduct(int productId) {
if (ProductType.productTypes[productId].weight > freeSpace)
return false;
products.put(productId, products.getOrDefault(productId, 0) + 1);
freeSpace -= ProductType.productTypes[productId].weight;
return true;
}
public void removeProduct(int productId) {
products.put(productId, products.get(productId) - 1);
freeSpace += ProductType.productTypes[productId].weight; }
public boolean Load(Warehouse warehouse, int productId, int quantity){
int qty = quantity;
int totalWeight = ProductType.productTypes[productId].weight * quantity;
int productQtyInWh = 0;
while (quantity > 0) {
productQtyInWh = warehouse.products[productId];
if (productQtyInWh >= quantity) {
productQtyInWh = quantity;
warehouse.products[productId] -= quantity;
quantity = 0;
} else {
if (productQtyInWh > 0) {
quantity -= productQtyInWh;
int time = position.distanceTo(warehouse.position) + 1;
position = warehouse.position;
TTL -= time;
if (TTL < 0) {
return false;
}
warehouse.products[productId] = 0;
Print(id, "L", warehouse.id, productId, productQtyInWh);
//return false;
}
if (Warehouse.warehouses.length == warehouse.vid + 1) {
warehouse = Warehouse.warehouses[0];
} else {
warehouse = Warehouse.warehouses[warehouse.vid + 1];
}
}
}
if (totalWeight > freeSpace){
return false;
}
int time = position.distanceTo(warehouse.position) + 1;
position = warehouse.position;
TTL -= time;
if (TTL < 0){
return false;
}
Print(id, "L", warehouse.id, productId, productQtyInWh);
freeSpace -= totalWeight;
currentProducts.put(productId, currentProducts.getOrDefault(productId, 0) + qty);
return true;
}
public void Print(int a, String b, int c, int d, int e){
Main.sb.append("\n" + a + " " + b + " " + c + " " + d + " " + e);
Main.CommandCount++;
}
@Override
public String toString() {
return "Drone{" +
"id=" + id +
", position=" + position +
", products=" + products +
", freeSpace=" + freeSpace +
'}';
}
public void addProduct(int id, int canTake) {
products.put(id, products.getOrDefault(id, 0) + canTake);
freeSpace -= ProductType.productTypes[id].weight * canTake; }
public boolean DeliverAll(Warehouse firstWarehouse, Order order) {
int time = position.distanceTo(order.point);
position = order.point;
TTL -= time;
if (TTL < 0){
return false;
}
Set<Integer> integers = currentProducts.keySet();
int[] values = new int[currentProducts.keySet().size()];
int varrr = 0;
for (Integer productId : integers){
values[varrr] = productId;
varrr++;
}
for(int product : values){
Integer productQty = currentProducts.get(product);
if (!Deliver(order, product, productQty)){
return false;
}
freeSpace += ProductType.productTypes[product].weight * productQty;
if (currentProducts.get(product) == productQty){
currentProducts.remove(product);
} else {
currentProducts.put(product, currentProducts.get(product) - productQty);
}
}
return true;
}
public boolean Deliver(Order order, int product, Integer integer){
TTL--;
if (TTL < 0) {
return false;
}
Print(id, "D", order.id, product, integer);
return true;
}
public void UnLoadAll(Warehouse firstWarehouse) {
for(int productId : currentProducts.keySet()){
TTL--;
Print(id, "U", firstWarehouse.id, productId, currentProducts.get(productId));
}
currentProducts = new HashMap<Integer, Integer>();
}
}