-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC242018.java
360 lines (314 loc) Β· 13 KB
/
AoC242018.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.adventofcode.aoc2018;
import static com.adventofcode.utils.Utils.EMPTY;
import static com.adventofcode.utils.Utils.atol;
import static com.adventofcode.utils.Utils.extractIntegerFromString;
import static com.adventofcode.utils.Utils.itoa;
import static com.adventofcode.utils.Utils.splitOnTabOrSpace;
import com.adventofcode.Solution;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Stream;
class AoC242018 implements Solution {
private static String solve(final List<String> input, final Predicate<Boolean> stop) {
int i = 1;
String row;
final Set<Group.GroupBuilder> immuneSystemBuilders = new HashSet<>();
while (!(row = input.get(i++)).startsWith("Infection")) {
initializeState(row, immuneSystemBuilders, Group.Army.IMMUNE_SYSTEM);
}
final Set<Group.GroupBuilder> infectionBuilders = new HashSet<>();
while (i < input.size()) {
initializeState(input.get(i++), infectionBuilders, Group.Army.INFECTION);
}
Set<Group> infection;
Set<Group> immuneSystem;
boolean immuneSystemWon = false;
boolean boost = false;
do {
final SortedSet<Group> targetSelectionPhase = new TreeSet<>(Comparator.comparingLong(Group::getEffectivePower)
.thenComparingLong(Group::getInitiative).reversed());
infection = buildArmy(infectionBuilders, false);
targetSelectionPhase.addAll(infection);
immuneSystem = buildArmy(immuneSystemBuilders, boost);
targetSelectionPhase.addAll(immuneSystem);
final SortedMap<Group, Group> attackingPhase = new TreeMap<>(Comparator.comparingLong(Group::getInitiative)
.reversed());
while (!immuneSystem.isEmpty() && !infection.isEmpty()) {
targetSelectionPhase(targetSelectionPhase, attackingPhase, infection, immuneSystem);
final long killed = attackingPhase(targetSelectionPhase, attackingPhase, infection, immuneSystem);
if (killed == 0) {
//stall: now winners
break;
}
}
if (infection.isEmpty()) {
immuneSystemWon = true;
} else {
//we need a boost
boost = true;
}
} while (!stop.test(immuneSystemWon));
long res = 0L;
final Set<Group> winner = immuneSystem.isEmpty() ? infection : immuneSystem;
for (final Group g : winner) {
res += g.getUnits();
}
return itoa(res);
}
private static Set<Group> buildArmy(final Set<Group.GroupBuilder> builders, final boolean boost) {
final Set<Group> army = new HashSet<>();
for (final Group.GroupBuilder g : builders) {
if (boost) {
//add boost
g.increaseAttackDamage();
}
army.add(g.build());
}
return army;
}
private static void targetSelectionPhase(final SortedSet<Group> targetSelectionPhase, final SortedMap<Group, Group> attackingPhase, final Set<Group> infection, final Set<Group> immuneSystem) {
for (final Group selecting : targetSelectionPhase) {
//select defending army
final Set<Group> enemies = selecting.getArmy() == Group.Army.INFECTION ? immuneSystem : infection;
Optional<Group> selectedOpt = Optional.empty();
long selectedDamage = 0L;
//choose defending group
for (final Group defending : enemies) {
final long potentialDamage = computeDamage(selecting, defending);
if (potentialDamage == 0) {
//no reason to attack if no damage is inflicted
continue;
}
if (selectedOpt.isPresent()) {
Group selected = selectedOpt.get();
if (potentialDamage > selectedDamage
|| potentialDamage == selectedDamage
&& (defending.getEffectivePower() > selected.getEffectivePower()
|| defending.getEffectivePower() == selected.getEffectivePower()
&& defending.getInitiative() > selected.getInitiative())) {
selectedOpt = Optional.of(defending);
selectedDamage = potentialDamage;
}
} else {
selectedOpt = Optional.of(defending);
selectedDamage = potentialDamage;
}
}
selectedOpt.ifPresent(selected -> {
//who is attacking who
attackingPhase.put(selecting, selected);
//the chosen defending group can't be attacked by anyone else
enemies.remove(selected);
});
}
}
private static long attackingPhase(final SortedSet<Group> targetSelectionPhase, final SortedMap<Group, Group> attackingPhase, final Set<Group> infection, final Set<Group> immuneSystem) {
long res = 0L;
for (final Map.Entry<Group, Group> attack : attackingPhase.entrySet()) {
final Group defending = attack.getValue();
//the defending group will not be able to attack anymore, unless it survives
targetSelectionPhase.remove(defending);
//compute how many units were killed
final long damage = computeDamage(attack.getKey(), defending);
final long killed = Math.min(damage / defending.getHitPoints(), defending.getUnits());
defending.removeUnits(killed);
res += killed;
if (defending.getUnits() > 0) {
//the defending group will be able to attack again because it survived
targetSelectionPhase.add(defending);
//the defending group can be attacked again because it survived
final Set<Group> army = defending.getArmy() == Group.Army.INFECTION ? infection : immuneSystem;
army.add(defending);
}
}
//attacking phase completed
attackingPhase.clear();
return res;
}
private static void initializeState(final String row, final Set<Group.GroupBuilder> army, final Group.Army armyType) {
if (row.isEmpty()) return;
final Group.GroupBuilder builder = new Group.GroupBuilder().setArmy(armyType);
String[] tmp = row.split("units");
builder.setUnits(atol(tmp[0].trim()));
tmp = tmp[1].split("hit");
builder.setHitPoints( extractIntegerFromString( tmp[0] ) );
int beginIndex = row.indexOf('(');
if (beginIndex >= 0) {
final int midIndex = row.indexOf(';');
final int endIndex = row.indexOf(')');
if (midIndex >= 0) {
extractQualities(splitOnTabOrSpace(row.substring(beginIndex + 1, midIndex)), builder);
beginIndex = midIndex + 1;
}
extractQualities(splitOnTabOrSpace(row.substring(beginIndex + 1, endIndex)), builder);
}
final List<String> rest = splitOnTabOrSpace(tmp[1].split("does")[1]);
builder.setAttackDamage(atol(rest.get(1))).setAttack(rest.get(2)).setInitiative(atol(rest.get(6)));
army.add(builder);
}
private static void extractQualities(final List<String> str, final Group.GroupBuilder builder) {
final Set<String> qualities = new HashSet<>();
for (int i = 2; i < str.size(); i++) {
final String quality = str.get(i);
qualities.add(quality.replace(",", EMPTY));
}
if (str.get(0).equals("weak")) {
builder.setWeaknesses(qualities);
} else {
builder.setImmunities(qualities);
}
}
private static long computeDamage(final Group attacking, final Group defending) {
final String attack = attacking.getAttack();
if (defending.getImmunities().contains(attack)) {
//defending group is immune == no damage
return 0;
} else {
final long damage = attacking.getEffectivePower();
//defending group is weak == double damage
if ((defending.getWeaknesses().contains(attack))) {
return damage * 2;
} else {
return damage;
}
}
}
@Override
public String solveFirstPart(final Stream<String> input) {
return solve(input.toList(), b -> true);
}
@Override
public String solveSecondPart(final Stream<String> input) {
return solve(input.toList(), b -> b);
}
private static class Group {
final long hitPoints;
final Set<String> weaknesses;
final Set<String> immunities;
final String attack;
final long attackDamage;
final long initiative;
final Army army;
long units;
private Group(final GroupBuilder groupBuilder) {
this.units = groupBuilder.units;
this.hitPoints = groupBuilder.hitPoints;
this.weaknesses = groupBuilder.weaknesses;
this.immunities = groupBuilder.immunities;
this.attack = groupBuilder.attack;
this.attackDamage = groupBuilder.attackDamage;
this.initiative = groupBuilder.initiative;
this.army = groupBuilder.army;
}
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Group group = (Group) o;
return getHitPoints() == group.getHitPoints() &&
getAttackDamage() == group.getAttackDamage() &&
getInitiative() == group.getInitiative() &&
getWeaknesses().equals(group.getWeaknesses()) &&
getImmunities().equals(group.getImmunities()) &&
getAttack().equals(group.getAttack()) &&
getArmy() == group.getArmy();
}
@Override
public int hashCode() {
return Objects.hash(getHitPoints(), getWeaknesses(), getImmunities(), getAttack(), getAttackDamage(),
getInitiative(), getArmy());
}
void removeUnits(final long units) {
this.units -= units;
}
long getEffectivePower() {
return units * attackDamage;
}
long getUnits() {
return units;
}
long getHitPoints() {
return hitPoints;
}
Set<String> getWeaknesses() {
return weaknesses;
}
Set<String> getImmunities() {
return immunities;
}
String getAttack() {
return attack;
}
long getAttackDamage() {
return attackDamage;
}
long getInitiative() {
return initiative;
}
Army getArmy() {
return army;
}
private enum Army {
INFECTION, IMMUNE_SYSTEM
}
static class GroupBuilder {
private long units;
private long hitPoints;
private Set<String> weaknesses = new HashSet<>();
private Set<String> immunities = new HashSet<>();
private String attack;
private long attackDamage;
private long initiative;
private Army army;
GroupBuilder setUnits(final long units) {
this.units = units;
return this;
}
GroupBuilder setHitPoints(final long hitPoints) {
this.hitPoints = hitPoints;
return this;
}
GroupBuilder setWeaknesses(final Set<String> weaknesses) {
this.weaknesses = weaknesses;
return this;
}
GroupBuilder setImmunities(final Set<String> immunities) {
this.immunities = immunities;
return this;
}
GroupBuilder setAttack(final String attack) {
this.attack = attack;
return this;
}
GroupBuilder setAttackDamage(final long attackDamage) {
this.attackDamage = attackDamage;
return this;
}
GroupBuilder increaseAttackDamage() {
this.attackDamage++;
return this;
}
GroupBuilder setInitiative(final long initiative) {
this.initiative = initiative;
return this;
}
GroupBuilder setArmy(final Army army) {
this.army = army;
return this;
}
Group build() {
return new Group(this);
}
}
}
}