-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay24.java
298 lines (238 loc) · 8.17 KB
/
Day24.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package aoc18;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import myutils18.ImmuneSystemGroup.Type;
import myutils18.ImmuneSystemGroup;
public class Day24 {
private List<ImmuneSystemGroup> allGroups;
private final File inputFile;
public Day24(File inputFile) throws IOException {
this.inputFile = inputFile;
allGroups = getAllGroups(inputFile, 0);
}
// part 1
/**
*
* @return sum of all surviving units of the winning side
*/
public int run() {
List<ImmuneSystemGroup> winners = getWinners();
System.out.println(winners.stream().findAny().get().type() + " " + winners);
return winners.stream().mapToInt(n -> n.units()).sum();
}
// part 2
// runs in O(log(n) * (max - min)), could be improved but the performance is
// fine for the given input
/**
*
* @return units after immune system wins for the first time
* @throws IOException
*/
public int run2() throws IOException {
int immuneSystemBoost = 1;
int min = 0;
int max = immuneSystemBoost;
// get lower and upper bound
while (!isImmuneSystem(getWinners())) {
min = max;
max *= 2;
allGroups = getAllGroups(inputFile, max);
}
// target is within min ... max
List<ImmuneSystemGroup> winners = null;
for (int i = min + 1; i <= max; i++) {
allGroups = getAllGroups(inputFile, i);
winners = getWinners();
if (winners != null)
if (isImmuneSystem(winners)) {
break;
}
}
return winners.stream().mapToInt(n -> n.units()).sum();
}
private boolean isImmuneSystem(List<ImmuneSystemGroup> winners) {
if (winners == null)
return false;
return winners.stream().findAny().get().type() == Type.IMMUNE_SYSTEM;
}
/**
*
* @return list containing the surviving units of the winning side
*/
public List<ImmuneSystemGroup> getWinners() {
List<ImmuneSystemGroup> activeImmuneSystem = new ArrayList<ImmuneSystemGroup>();
List<ImmuneSystemGroup> activeInfection = new ArrayList<ImmuneSystemGroup>();
for (ImmuneSystemGroup group : allGroups) {
if (group.type() == Type.IMMUNE_SYSTEM) {
activeImmuneSystem.add(group);
} else
activeInfection.add(group);
}
while (!activeImmuneSystem.isEmpty() && !activeInfection.isEmpty()) {
allGroups.sort(new TargetSelectionComparator());
activeImmuneSystem.sort(new TargetSelectionComparator());
activeInfection.sort(new TargetSelectionComparator());
// select targets for each group
ImmuneSystemGroup[] attackers = new ImmuneSystemGroup[activeImmuneSystem.size() + activeInfection.size()];
Map<ImmuneSystemGroup, ImmuneSystemGroup> attackerAndTargets = new HashMap<ImmuneSystemGroup, ImmuneSystemGroup>();
int index = 0;
for (ImmuneSystemGroup group : allGroups) {
if (group.units() > 0 && (activeImmuneSystem.contains(group) || activeInfection.contains(group))) {
attackers[index++] = group;
}
}
Set<ImmuneSystemGroup> unavailableTargets = new HashSet<ImmuneSystemGroup>();
for (int i = 0; i < attackers.length; i++) {
if (attackers[i].type() == Type.IMMUNE_SYSTEM) {
attackerAndTargets.put(attackers[i], getTarget(attackers[i], unavailableTargets, activeInfection));
} else {
attackerAndTargets.put(attackers[i],
getTarget(attackers[i], unavailableTargets, activeImmuneSystem));
}
}
Arrays.sort(attackers, new AttackOrderComparator());
int deadUnits = 0;
for (ImmuneSystemGroup attacker : attackers) {
ImmuneSystemGroup target = attackerAndTargets.get(attacker);
if (attacker.units() > 0 && target != null && target.units() > 0) {
deadUnits += attacker.attack(target);
if (target.units() <= 0) {
if (target.type() == Type.IMMUNE_SYSTEM)
activeImmuneSystem.remove(target);
else
activeInfection.remove(target);
}
}
}
// stalemate
if (deadUnits == 0)
break;
}
// stalemate
if (activeInfection.size() > 0 && activeImmuneSystem.size() > 0) {
return null;
}
List<ImmuneSystemGroup> survivors = activeInfection.size() > 0 ? activeInfection : activeImmuneSystem;
return survivors;
}
private ImmuneSystemGroup getTarget(ImmuneSystemGroup attacker, Set<ImmuneSystemGroup> markedTargets,
List<ImmuneSystemGroup> targets) {
ImmuneSystemGroup target = null;
int maxDamage = 0;
for (ImmuneSystemGroup group : targets) {
if (!markedTargets.contains(group) && !group.immunities().contains(attacker.attackType())) {
int damageTo = attacker.getEffPower();
if (group.weaknesses().contains(attacker.attackType())) {
damageTo *= 2;
}
if (damageTo > maxDamage) {
target = group;
maxDamage = damageTo;
}
}
}
markedTargets.add(target);
return target;
}
// returns a list of all groups
/**
*
* @param inputFile
* input file containing the unit descriptions
* @param immuneSystemBoost
* integer value that gets added to the attack power of all
* immune system groups. should be set to 0 for part 1
* @return allGroups List containing all groups
* @throws IOException
*/
private List<ImmuneSystemGroup> getAllGroups(File inputFile, int immuneSystemBoost) throws IOException {
List<ImmuneSystemGroup> allGroups = new ArrayList<ImmuneSystemGroup>();
BufferedReader br = new BufferedReader(new FileReader(inputFile));
String line = " ";
Type currentType = Type.IMMUNE_SYSTEM;
while ((line = br.readLine()) != null) {
if (line.length() == 0) {
continue;
}
if (line.contains("Infection")) {
currentType = Type.INFECTION;
continue;
} else if (line.contains("Immune System")) {
currentType = Type.IMMUNE_SYSTEM;
continue;
}
int units = Integer.parseInt(line.substring(0, line.indexOf('u') - 1));
int hitpoints = Integer.parseInt(line.substring(line.indexOf("with") + 5, line.indexOf("hit") - 1));
Set<String> weakTo = new HashSet<String>();
Set<String> immuneTo = new HashSet<String>();
// get weaknesses and immunities
if (line.indexOf('(') != -1) {
String[] weaknessImmunity = line.substring(line.indexOf('(') + 1, line.indexOf(')')).split(" ");
String type = "";
for (String str : weaknessImmunity) {
str = str.replaceAll("[^a-zA-Z]+", "");
if (str.equals("immune") || str.equals("weak")) {
type = str;
} else if (!str.equals("to")) {
if (type.equals("immune")) {
immuneTo.add(str);
} else if (type.endsWith("weak"))
weakTo.add(str);
}
}
}
String[] atkTypeInitiative = line.substring(line.indexOf("does") + 4, line.length()).trim().split(" ");
int attackPower = Integer.parseInt(atkTypeInitiative[0]);
String attackType = atkTypeInitiative[1];
int initiative = Integer.parseInt(atkTypeInitiative[atkTypeInitiative.length - 1]);
if (currentType == Type.IMMUNE_SYSTEM) {
attackPower += immuneSystemBoost;
}
allGroups.add(new ImmuneSystemGroup(currentType, units, hitpoints, weakTo, immuneTo, attackType,
attackPower, initiative));
}
br.close();
return allGroups;
}
public static void main(String[] args) throws IOException {
Day24 test = new Day24(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2018\\Day 24\\InputFile1.txt"));
int res2 = test.run2();
System.out.println(res2);
}
public class TargetSelectionComparator implements Comparator<ImmuneSystemGroup> {
@Override
public int compare(ImmuneSystemGroup i0, ImmuneSystemGroup i1) {
if (i0.getEffPower() < i1.getEffPower()) {
return 1;
}
if (i0.getEffPower() == i1.getEffPower()) {
if (i0.initiative() < i1.initiative()) {
return 1;
}
return -1;
}
return -1;
}
}
public class AttackOrderComparator implements Comparator<ImmuneSystemGroup> {
@Override
public int compare(ImmuneSystemGroup i0, ImmuneSystemGroup i1) {
if (i0.initiative() < i1.initiative()) {
return 1;
}
if (i0.initiative() == i1.initiative())
return 0;
return -1;
}
}
}