-
Notifications
You must be signed in to change notification settings - Fork 6
/
SpringChalleng2023.java
134 lines (122 loc) · 5.54 KB
/
SpringChalleng2023.java
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
import java.util.*;
import java.util.stream.Collectors;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
List<Cell> cells = new ArrayList<>();
int numberOfCells = in.nextInt(); // amount of hexagonal cells in this map
for (int i = 0; i < numberOfCells; i++) {
int type = in.nextInt(); // 0 for empty, 1 for eggs, 2 for crystal
int initialResources = in.nextInt(); // the initial amount of eggs/crystals on this cell
// the index of the neighbouring cell for each direction
List<Integer> neighbors = new ArrayList<>();
neighbors.add(in.nextInt());
neighbors.add(in.nextInt());
neighbors.add(in.nextInt());
neighbors.add(in.nextInt());
neighbors.add(in.nextInt());
neighbors.add(in.nextInt());
Cell cell = new Cell(i, type, initialResources, neighbors, 0, 0);
System.err.println(cell);
cells.add(cell);
}
int numberOfBases = in.nextInt();
List<Integer> myBases = new ArrayList<>();
for (int i = 0; i < numberOfBases; i++) {
myBases.add(in.nextInt());
}
List<Integer> oppBases = new ArrayList<>();
for (int i = 0; i < numberOfBases; i++) {
oppBases.add(in.nextInt());
}
// game loop
while (true) {
StringBuilder stringBuilder = null;
List<Cell> cellsResourcesNeighbors = new ArrayList<>();
int numberMyAnts = 0;
for (int i = 0; i < numberOfCells; i++) {
int resources = in.nextInt(); // the current amount of eggs/crystals on this cell
int myAnts = in.nextInt(); // the amount of your ants on this cell
int oppAnts = in.nextInt(); // the amount of opponent ants on this cell
cells.get(i).resources = resources;
cells.get(i).myAnts = myAnts;
cells.get(i).oppAnts = oppAnts;
numberMyAnts += myAnts;
}
Map<Integer, List<Cell>> hmap = new HashMap<>();
for (int i = 0; i < myBases.size(); i++) {
cellsResourcesNeighbors = new ArrayList<>();
List<Integer> neighborsRest = new ArrayList<>();
List<Integer> neighborsVist = new ArrayList<>();
int myCellBase = myBases.get(i);
neighborsRest.addAll(cells.get(myCellBase).neighbors.stream().filter(voi -> voi != -1).collect(Collectors.toList()));
while (!neighborsRest.isEmpty()) {
Cell cell = cells.get(neighborsRest.get(0));
neighborsRest.remove(0);
boolean isOppAntBase = oppBases.contains(cell.index);
if (!isOppAntBase && cell.cellType != 0 && cell.resources != 0 && !neighborsVist.contains(cell.index)) {
cellsResourcesNeighbors.add(cell);
//System.err.println("Add resources voisin" + cell);
}
neighborsVist.add(cell.index);
neighborsRest.addAll(cells.get(cell.index).neighbors.stream().filter(voi -> (voi != -1 && !neighborsVist.contains(voi))).collect(Collectors.toList()));
}
hmap.put(i, cellsResourcesNeighbors);
}
int strength = 1;
for (Map.Entry entry : hmap.entrySet()) {
cellsResourcesNeighbors = (List<Cell>) entry.getValue();
int myCellBase = myBases.get((int) entry.getKey());
if (!cellsResourcesNeighbors.isEmpty()) {
int size = Math.min(cellsResourcesNeighbors.size(), numberMyAnts / (numberOfCells / 8) + 1);
for (int i = 0; i < size; i++) {
//System.err.println("Add LINE");
if (stringBuilder == null) {
System.err.println("Add StringBuilder");
stringBuilder = new StringBuilder("LINE " + cellsResourcesNeighbors.get(i).index + " " + myCellBase + " " + strength);
} else {
stringBuilder.append(";LINE " + cellsResourcesNeighbors.get(i).index + " " + myCellBase + " " + strength);
}
}
}
}
// WAIT | LINE <sourceIdx> <targetIdx> <strength> | BEACON <cellIdx> <strength> | MESSAGE <text>
if (stringBuilder == null) {
System.out.println("WAIT");
} else {
System.out.println(stringBuilder.toString());
}
}
}
}
class Cell {
int index;
int cellType;
int resources;
List<Integer> neighbors;
int myAnts;
int oppAnts;
public Cell(int index, int cellType, int resources, List<Integer> neighbors, int myAnts, int oppAnts) {
this.index = index;
this.cellType = cellType;
this.resources = resources;
this.neighbors = neighbors;
this.myAnts = myAnts;
this.oppAnts = oppAnts;
}
@Override
public String toString() {
return "Cell{" +
"index=" + index +
", cellType=" + cellType +
", resources=" + resources +
", neighbors=" + neighbors +
", myAnts=" + myAnts +
", oppAnts=" + oppAnts +
'}';
}
}