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
|
--- Day 15: Beverage Bandits ---
Having perfected their hot chocolate, the Elves have a new problem: the Goblins that live in these
caves will do anything to steal it. Looks like they're here for a fight.
You scan the area, generating a map of the walls (#), open cavern (.), and starting position of
every Goblin (G) and Elf (E) (your puzzle input).
Combat proceeds in [1m[97mrounds[0m; in each round, each unit that is still alive takes a [1m[97mturn[0m, resolving all
of its actions before the next unit's turn begins. On each unit's turn, it tries to
[1m[97mmove[0m into range of an enemy (if it isn't already) and then [1m[97mattack[0m (if it is in range).
All units are very disciplined and always follow very strict combat rules. Units never move or
attack diagonally, as doing so would be dishonorable. When multiple choices are equally valid, ties
are broken in [1m[97mreading order[0m: top-to-bottom, then left-to-right. For instance, the order in which
units take their turns within a round is the [1m[97mreading order of their starting positions[0m in that
round, regardless of the type of unit or whether other units have moved after the round started.
For example:
would take their
These units: turns in this order:
####### #######
#.G.E.# #.1.2.#
#E.G.E# #3.4.5#
#.G.E.# #.6.7.#
####### #######
Each unit begins its turn by identifying all possible [1m[97mtargets[0m (enemy units). If no targets remain,
combat ends.
Then, the unit identifies all of the open squares (.) that are [1m[97min range[0m of each target; these are
the squares which are [1m[97madjacent[0m (immediately up, down, left, or right) to any target and which aren't
already occupied by a wall or another unit. Alternatively, the unit might [1m[97malready[0m be in range of a
target. If the unit is not already in range of a target, and there are no open squares which are in
range of a target, the unit ends its turn.
If the unit is already in range of a target, it does not [1m[97mmove[0m, but continues its turn with an
[1m[97mattack[0m. Otherwise, since it is not in range of a target, it [1m[97mmoves[0m.
To [1m[97mmove[0m, the unit first considers the squares that are [1m[97min range[0m and determines [1m[97mwhich of those
squares it could reach in the fewest steps[0m. A [1m[97mstep[0m is a single movement to any [1m[97madjacent[0m (immediately
up, down, left, or right) open (.) square. Units cannot move into walls or other units. The unit
does this while considering the [1m[97mcurrent positions of units[0m and does [1m[97mnot[0m do any prediction about
where units will be later. If the unit cannot reach (find an open path to) any of the squares that
are in range, it ends its turn. If multiple squares are in range and [1m[97mtied[0m for being reachable in the
fewest steps, the square which is first in [1m[97mreading order[0m is chosen. For example:
Targets: In range: Reachable: Nearest: Chosen:
####### ####### ####### ####### #######
#E..G.# #E.?G?# #E.@G.# #E.!G.# #E.+G.#
#...#.# --> #.?.#?# --> #.@.#.# --> #.!.#.# --> #...#.#
#.G.#G# #?G?#G# #@G@#G# #!G.#G# #.G.#G#
####### ####### ####### ####### #######
In the above scenario, the Elf has three targets (the three Goblins):
- Each of the Goblins has open, adjacent squares which are [1m[97min range[0m (marked with a ? on the map).
- Of those squares, four are [1m[97mreachable[0m (marked @); the other two (on the right) would require
moving through a wall or unit to reach.
- Three of these reachable squares are [1m[97mnearest[0m, requiring the fewest steps (only 2) to reach
(marked !).
- Of those, the square which is first in reading order is [1m[97mchosen[0m (+).
The unit then takes a single [1m[97mstep[0m toward the chosen square along the [1m[97mshortest path[0m to that square.
If multiple steps would put the unit equally closer to its destination, the unit chooses the step
which is first in reading order. (This requires knowing when there is [1m[97mmore than one shortest
path[0m so that you can consider the first step of each such path.) For example:
In range: Nearest: Chosen: Distance: Step:
####### ####### ####### ####### #######
#.E...# #.E...# #.E...# #4E[1m[97m2[0m12# #..E..#
#...?.# --> #...!.# --> #...+.# --> #3[1m[97m2[0m101# --> #.....#
#..?G?# #..!G.# #...G.# #432G2# #...G.#
####### ####### ####### ####### #######
The Elf sees three squares in range of a target (?), two of which are nearest (!), and so the first
in reading order is chosen (+). Under "Distance", each open square is marked with its distance from
the destination square; the two squares to which the Elf could move on this turn (down and to the
right) are both equally good moves and would leave the Elf 2 steps from being in range of the
Goblin. Because the step which is first in reading order is chosen, the Elf moves [1m[97mright[0m one square.
Here's a larger example of movement:
Initially:
#########
#G..G..G#
#.......#
#.......#
#G..E..G#
#.......#
#.......#
#G..G..G#
#########
After 1 round:
#########
#.G...G.#
#...G...#
#...E..G#
#.G.....#
#.......#
#G..G..G#
#.......#
#########
After 2 rounds:
#########
#..G.G..#
#...G...#
#.G.E.G.#
#.......#
#G..G..G#
#.......#
#.......#
#########
After 3 rounds:
#########
#.......#
#..GGG..#
#..GEG..#
#G..G...#
#......G#
#.......#
#.......#
#########
Once the Goblins and Elf reach the positions above, they all are either in range of a target or
cannot find any square in range of a target, and so none of the units can move until a unit dies.
After moving (or if the unit began its turn in range of a target), the unit [1m[97mattacks[0m.
To [1m[97mattack[0m, the unit first determines [1m[97mall[0m of the targets that are [1m[97min range[0m of it by being immediately
[1m[97madjacent[0m to it. If there are no such targets, the unit ends its turn. Otherwise, the adjacent target
with the [1m[97mfewest hit points[0m is selected; in a tie, the adjacent target with the fewest hit points
which is first in reading order is selected.
The unit deals damage equal to its [1m[97mattack power[0m to the selected target, reducing its hit points by
that amount. If this reduces its hit points to 0 or fewer, the selected target [1m[97mdies[0m: its square
becomes . and it takes no further turns.
Each [1m[97munit[0m, either Goblin or Elf, has 3 [1m[97mattack power[0m and starts with 200 [1m[97mhit points[0m.
For example, suppose the only Elf is about to attack:
HP: HP:
G.... 9 G.... 9
..G.. 4 ..G.. 4
..E[1m[97mG[0m. 2 --> ..E..
..G.. 2 ..G.. 2
...G. 1 ...G. 1
The "HP" column shows the hit points of the Goblin to the left in the corresponding row. The Elf is
in range of three targets: the Goblin above it (with 4 hit points), the Goblin to its right (with 2
hit points), and the Goblin below it (also with 2 hit points). Because three targets are in range,
the ones with the lowest hit points are selected: the two Goblins with 2 hit points each (one to the
right of the Elf and one below the Elf). Of those, the Goblin first in reading order (the one to the
right of the Elf) is selected. The selected Goblin's hit points (2) are reduced by the Elf's attack
power (3), reducing its hit points to -1, killing it.
After attacking, the unit's turn ends. Regardless of how the unit's turn ends, the next unit in the
round takes its turn. If all units have taken turns in this round, the round ends, and a new round
begins.
The Elves look quite outnumbered. You need to determine the [1m[97moutcome[0m of the battle: the
[1m[97mnumber of full rounds that were completed[0m (not counting the round in which combat ends) multiplied
by [1m[97mthe sum of the hit points of all remaining units[0m at the moment combat ends. (Combat only ends
when a unit finds no targets during its turn.)
Below is an entire sample combat. Next to each map, each row's units' hit points are listed from
left to right.
Initially:
#######
#.G...# G(200)
#...EG# E(200), G(200)
#.#.#G# G(200)
#..G#E# G(200), E(200)
#.....#
#######
After 1 round:
#######
#..G..# G(200)
#...EG# E(197), G(197)
#.#G#G# G(200), G(197)
#...#E# E(197)
#.....#
#######
After 2 rounds:
#######
#...G.# G(200)
#..GEG# G(200), E(188), G(194)
#.#.#G# G(194)
#...#E# E(194)
#.....#
#######
Combat ensues; eventually, the top Elf dies:
After 23 rounds:
#######
#...G.# G(200)
#..G.G# G(200), G(131)
#.#.#G# G(131)
#...#E# E(131)
#.....#
#######
After 24 rounds:
#######
#..G..# G(200)
#...G.# G(131)
#.#G#G# G(200), G(128)
#...#E# E(128)
#.....#
#######
After 25 rounds:
#######
#.G...# G(200)
#..G..# G(131)
#.#.#G# G(125)
#..G#E# G(200), E(125)
#.....#
#######
After 26 rounds:
#######
#G....# G(200)
#.G...# G(131)
#.#.#G# G(122)
#...#E# E(122)
#..G..# G(200)
#######
After 27 rounds:
#######
#G....# G(200)
#.G...# G(131)
#.#.#G# G(119)
#...#E# E(119)
#...G.# G(200)
#######
After 28 rounds:
#######
#G....# G(200)
#.G...# G(131)
#.#.#G# G(116)
#...#E# E(113)
#....G# G(200)
#######
More combat ensues; eventually, the bottom Elf dies:
After 47 rounds:
#######
#G....# G(200)
#.G...# G(131)
#.#.#G# G(59)
#...#.#
#....G# G(200)
#######
Before the 48th round can finish, the top-left Goblin finds that there are no targets remaining, and
so combat ends. So, the number of [1m[97mfull rounds[0m that were completed is [1m[97m47[0m, and the sum of the hit
points of all remaining units is 200+131+59+200 = [1m[97m590[0m. From these, the [1m[97moutcome[0m of the battle is 47 *
590 = [1m[97m27730[0m.
Here are a few example summarized combats:
####### #######
#G..#E# #...#E# E(200)
#E#E.E# #E#...# E(197)
#G.##.# --> #.E##.# E(185)
#...#E# #E..#E# E(200), E(200)
#...E.# #.....#
####### #######
Combat ends after 37 full rounds
Elves win with 982 total hit points left
Outcome: 37 * 982 = [1m[97m36334[0m
####### #######
#E..EG# #.E.E.# E(164), E(197)
#.#G.E# #.#E..# E(200)
#E.##E# --> #E.##.# E(98)
#G..#.# #.E.#.# E(200)
#..E#.# #...#.#
####### #######
Combat ends after 46 full rounds
Elves win with 859 total hit points left
Outcome: 46 * 859 = [1m[97m39514[0m
####### #######
#E.G#.# #G.G#.# G(200), G(98)
#.#G..# #.#G..# G(200)
#G.#.G# --> #..#..#
#G..#.# #...#G# G(95)
#...E.# #...G.# G(200)
####### #######
Combat ends after 35 full rounds
Goblins win with 793 total hit points left
Outcome: 35 * 793 = [1m[97m27755[0m
####### #######
#.E...# #.....#
#.#..G# #.#G..# G(200)
#.###.# --> #.###.#
#E#G#G# #.#.#.#
#...#G# #G.G#G# G(98), G(38), G(200)
####### #######
Combat ends after 54 full rounds
Goblins win with 536 total hit points left
Outcome: 54 * 536 = [1m[97m28944[0m
######### #########
#G......# #.G.....# G(137)
#.E.#...# #G.G#...# G(200), G(200)
#..##..G# #.G##...# G(200)
#...##..# --> #...##..#
#...#...# #.G.#...# G(200)
#.G...G.# #.......#
#.....G.# #.......#
######### #########
Combat ends after 20 full rounds
Goblins win with 937 total hit points left
Outcome: 20 * 937 = [1m[97m18740[0m
[1m[97mWhat is the outcome[0m of the combat described in your puzzle input?
|