-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay15.java
274 lines (233 loc) · 7.69 KB
/
Day15.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
package com.sbaars.adventofcode.year18.days;
import com.sbaars.adventofcode.year18.Day2018;
import com.sbaars.adventofcode.common.location.Loc;
import java.util.*;
import java.util.stream.Collectors;
public class Day15 extends Day2018 {
private static final int GOBLIN_ATTACK_POWER = 3;
private static final int INITIAL_HP = 200;
private static class Unit {
Loc position;
int hp;
char type; // 'E' for Elf, 'G' for Goblin
boolean alive;
int attackPower;
Unit(Loc position, char type, int attackPower) {
this.position = position;
this.hp = INITIAL_HP;
this.type = type;
this.alive = true;
this.attackPower = attackPower;
}
boolean isEnemy(Unit other) {
return this.type != other.type;
}
}
private static class CombatResult {
final int rounds;
final int totalHp;
final boolean elvesWon;
final boolean elfDied;
CombatResult(int rounds, int totalHp, boolean elvesWon, boolean elfDied) {
this.rounds = rounds;
this.totalHp = totalHp;
this.elvesWon = elvesWon;
this.elfDied = elfDied;
}
int getOutcome() {
return rounds * totalHp;
}
}
private static class MutableBoolean {
boolean value;
MutableBoolean(boolean value) {
this.value = value;
}
}
public Day15() {
super(15);
}
public static void main(String[] args) {
new Day15().printParts();
}
private List<Unit> parseUnits(char[][] grid, int elfAttackPower) {
List<Unit> units = new ArrayList<>();
for (int y = 0; y < grid.length; y++) {
for (int x = 0; x < grid[y].length; x++) {
if (grid[y][x] == 'E' || grid[y][x] == 'G') {
units.add(new Unit(new Loc(x, y), grid[y][x],
grid[y][x] == 'E' ? elfAttackPower : GOBLIN_ATTACK_POWER));
}
}
}
return units;
}
private List<Loc> getAdjacentSpaces(Loc pos) {
List<Loc> adjacent = new ArrayList<>();
// Order: up, left, right, down (reading order for equal distances)
adjacent.add(new Loc(pos.x, pos.y - 1));
adjacent.add(new Loc(pos.x - 1, pos.y));
adjacent.add(new Loc(pos.x + 1, pos.y));
adjacent.add(new Loc(pos.x, pos.y + 1));
return adjacent;
}
private Optional<Loc> findNextMove(Unit unit, List<Unit> enemies, char[][] grid) {
if (enemies.isEmpty()) return Optional.empty();
// Check if already in range of an enemy
boolean inRange = false;
for (Loc adj : getAdjacentSpaces(unit.position)) {
if (isEnemy(adj, unit.type, grid)) {
inRange = true;
break;
}
}
if (inRange) return Optional.empty();
// Find all target squares (empty squares adjacent to enemies)
Set<Loc> targets = new HashSet<>();
for (Unit enemy : enemies) {
for (Loc adj : getAdjacentSpaces(enemy.position)) {
if (grid[adj.intY()][adj.intX()] == '.') {
targets.add(adj);
}
}
}
if (targets.isEmpty()) return Optional.empty();
// Find reachable targets using BFS
Map<Loc, Integer> distances = new HashMap<>();
Map<Loc, Loc> firstStep = new HashMap<>();
Queue<Loc> queue = new LinkedList<>();
// Add initial moves
for (Loc adj : getAdjacentSpaces(unit.position)) {
if (grid[adj.intY()][adj.intX()] == '.') {
distances.put(adj, 1);
firstStep.put(adj, adj);
queue.add(adj);
}
}
// BFS
while (!queue.isEmpty()) {
Loc current = queue.poll();
int dist = distances.get(current);
for (Loc next : getAdjacentSpaces(current)) {
if (grid[next.intY()][next.intX()] == '.' && !distances.containsKey(next)) {
distances.put(next, dist + 1);
firstStep.put(next, firstStep.get(current));
queue.add(next);
}
}
}
// Find nearest reachable target
Loc chosen = null;
int minDist = Integer.MAX_VALUE;
for (Loc target : targets) {
if (distances.containsKey(target)) {
int dist = distances.get(target);
if (dist < minDist ||
(dist == minDist && (target.y < chosen.y ||
(target.y == chosen.y && target.x < chosen.x)))) {
minDist = dist;
chosen = target;
}
}
}
return chosen == null ? Optional.empty() : Optional.of(firstStep.get(chosen));
}
private boolean isEnemy(Loc pos, char myType, char[][] grid) {
char c = grid[pos.intY()][pos.intX()];
return (myType == 'E' && c == 'G') || (myType == 'G' && c == 'E');
}
private Optional<Unit> findTarget(Unit unit, List<Unit> units, char[][] grid) {
Unit target = null;
int minHp = Integer.MAX_VALUE;
for (Loc adj : getAdjacentSpaces(unit.position)) {
if (isEnemy(adj, unit.type, grid)) {
for (Unit enemy : units) {
if (enemy.alive && enemy.position.equals(adj)) {
if (enemy.hp < minHp ||
(enemy.hp == minHp && (enemy.position.y < target.position.y ||
(enemy.position.y == target.position.y && enemy.position.x < target.position.x)))) {
minHp = enemy.hp;
target = enemy;
}
}
}
}
}
return Optional.ofNullable(target);
}
private char[][] copyGrid(char[][] original) {
char[][] copy = new char[original.length][];
for (int i = 0; i < original.length; i++) {
copy[i] = Arrays.copyOf(original[i], original[i].length);
}
return copy;
}
private CombatResult runCombat(char[][] initialGrid, int elfAttackPower) {
char[][] grid = copyGrid(initialGrid);
List<Unit> units = parseUnits(grid, elfAttackPower);
int rounds = 0;
final MutableBoolean elfDied = new MutableBoolean(false);
while (true) {
// Sort units in reading order
units.sort((a, b) -> {
if (a.position.y != b.position.y) return Long.compare(a.position.y, b.position.y);
return Long.compare(a.position.x, b.position.x);
});
boolean roundCompleted = true;
// Take a copy of the units list to handle deaths
for (Unit unit : new ArrayList<>(units)) {
if (!unit.alive) continue;
// Check if combat has ended
List<Unit> enemies = units.stream()
.filter(u -> u.alive && unit.isEnemy(u))
.collect(Collectors.toList());
if (enemies.isEmpty()) {
roundCompleted = false;
break;
}
// Move phase
findNextMove(unit, enemies, grid).ifPresent(newPos -> {
grid[unit.position.intY()][unit.position.intX()] = '.';
unit.position = newPos;
grid[newPos.intY()][newPos.intX()] = unit.type;
});
// Attack phase
findTarget(unit, units, grid).ifPresent(target -> {
target.hp -= unit.attackPower;
if (target.hp <= 0) {
target.alive = false;
if (target.type == 'E') elfDied.value = true;
grid[target.position.intY()][target.position.intX()] = '.';
}
});
}
if (!roundCompleted) {
int totalHp = units.stream()
.filter(u -> u.alive)
.mapToInt(u -> u.hp)
.sum();
boolean elvesWon = units.stream()
.anyMatch(u -> u.alive && u.type == 'E');
return new CombatResult(rounds, totalHp, elvesWon, elfDied.value);
}
rounds++;
}
}
@Override
public Object part1() {
CombatResult result = runCombat(dayGrid(), GOBLIN_ATTACK_POWER);
return result.getOutcome();
}
@Override
public Object part2() {
char[][] grid = dayGrid();
int elfAttackPower = 4;
while (true) {
CombatResult result = runCombat(grid, elfAttackPower);
if (result.elvesWon && !result.elfDied) {
return result.getOutcome();
}
elfAttackPower++;
}
}
}