part1 (11940B)
1--- Day 15: Beverage Bandits --- 2 3Having perfected their hot chocolate, the Elves have a new problem: the Goblins that live in these 4caves will do anything to steal it. Looks like they're here for a fight. 5 6You scan the area, generating a map of the walls (#), open cavern (.), and starting position of 7every Goblin (G) and Elf (E) (your puzzle input). 8 9Combat proceeds in [1m[97mrounds[0m; in each round, each unit that is still alive takes a [1m[97mturn[0m, resolving all 10of its actions before the next unit's turn begins. On each unit's turn, it tries to 11[1m[97mmove[0m into range of an enemy (if it isn't already) and then [1m[97mattack[0m (if it is in range). 12 13All units are very disciplined and always follow very strict combat rules. Units never move or 14attack diagonally, as doing so would be dishonorable. When multiple choices are equally valid, ties 15are broken in [1m[97mreading order[0m: top-to-bottom, then left-to-right. For instance, the order in which 16units take their turns within a round is the [1m[97mreading order of their starting positions[0m in that 17round, regardless of the type of unit or whether other units have moved after the round started. 18For example: 19 20 would take their 21These units: turns in this order: 22 ####### ####### 23 #.G.E.# #.1.2.# 24 #E.G.E# #3.4.5# 25 #.G.E.# #.6.7.# 26 ####### ####### 27 28Each unit begins its turn by identifying all possible [1m[97mtargets[0m (enemy units). If no targets remain, 29combat ends. 30 31Then, the unit identifies all of the open squares (.) that are [1m[97min range[0m of each target; these are 32the squares which are [1m[97madjacent[0m (immediately up, down, left, or right) to any target and which aren't 33already occupied by a wall or another unit. Alternatively, the unit might [1m[97malready[0m be in range of a 34target. If the unit is not already in range of a target, and there are no open squares which are in 35range of a target, the unit ends its turn. 36 37If the unit is already in range of a target, it does not [1m[97mmove[0m, but continues its turn with an 38[1m[97mattack[0m. Otherwise, since it is not in range of a target, it [1m[97mmoves[0m. 39 40To [1m[97mmove[0m, the unit first considers the squares that are [1m[97min range[0m and determines [1m[97mwhich of those 41squares it could reach in the fewest steps[0m. A [1m[97mstep[0m is a single movement to any [1m[97madjacent[0m (immediately 42up, down, left, or right) open (.) square. Units cannot move into walls or other units. The unit 43does this while considering the [1m[97mcurrent positions of units[0m and does [1m[97mnot[0m do any prediction about 44where units will be later. If the unit cannot reach (find an open path to) any of the squares that 45are in range, it ends its turn. If multiple squares are in range and [1m[97mtied[0m for being reachable in the 46fewest steps, the square which is first in [1m[97mreading order[0m is chosen. For example: 47 48Targets: In range: Reachable: Nearest: Chosen: 49####### ####### ####### ####### ####### 50#E..G.# #E.?G?# #E.@G.# #E.!G.# #E.+G.# 51#...#.# --> #.?.#?# --> #.@.#.# --> #.!.#.# --> #...#.# 52#.G.#G# #?G?#G# #@G@#G# #!G.#G# #.G.#G# 53####### ####### ####### ####### ####### 54 55In the above scenario, the Elf has three targets (the three Goblins): 56 57 58 - Each of the Goblins has open, adjacent squares which are [1m[97min range[0m (marked with a ? on the map). 59 60 - Of those squares, four are [1m[97mreachable[0m (marked @); the other two (on the right) would require 61moving through a wall or unit to reach. 62 63 - Three of these reachable squares are [1m[97mnearest[0m, requiring the fewest steps (only 2) to reach 64(marked !). 65 66 - Of those, the square which is first in reading order is [1m[97mchosen[0m (+). 67 68 69The unit then takes a single [1m[97mstep[0m toward the chosen square along the [1m[97mshortest path[0m to that square. 70If multiple steps would put the unit equally closer to its destination, the unit chooses the step 71which is first in reading order. (This requires knowing when there is [1m[97mmore than one shortest 72path[0m so that you can consider the first step of each such path.) For example: 73 74In range: Nearest: Chosen: Distance: Step: 75####### ####### ####### ####### ####### 76#.E...# #.E...# #.E...# #4E[1m[97m2[0m12# #..E..# 77#...?.# --> #...!.# --> #...+.# --> #3[1m[97m2[0m101# --> #.....# 78#..?G?# #..!G.# #...G.# #432G2# #...G.# 79####### ####### ####### ####### ####### 80 81The Elf sees three squares in range of a target (?), two of which are nearest (!), and so the first 82in reading order is chosen (+). Under "Distance", each open square is marked with its distance from 83the destination square; the two squares to which the Elf could move on this turn (down and to the 84right) are both equally good moves and would leave the Elf 2 steps from being in range of the 85Goblin. Because the step which is first in reading order is chosen, the Elf moves [1m[97mright[0m one square. 86 87Here's a larger example of movement: 88 89Initially: 90######### 91#G..G..G# 92#.......# 93#.......# 94#G..E..G# 95#.......# 96#.......# 97#G..G..G# 98######### 99 100After 1 round: 101######### 102#.G...G.# 103#...G...# 104#...E..G# 105#.G.....# 106#.......# 107#G..G..G# 108#.......# 109######### 110 111After 2 rounds: 112######### 113#..G.G..# 114#...G...# 115#.G.E.G.# 116#.......# 117#G..G..G# 118#.......# 119#.......# 120######### 121 122After 3 rounds: 123######### 124#.......# 125#..GGG..# 126#..GEG..# 127#G..G...# 128#......G# 129#.......# 130#.......# 131######### 132 133Once the Goblins and Elf reach the positions above, they all are either in range of a target or 134cannot find any square in range of a target, and so none of the units can move until a unit dies. 135 136After moving (or if the unit began its turn in range of a target), the unit [1m[97mattacks[0m. 137 138To [1m[97mattack[0m, the unit first determines [1m[97mall[0m of the targets that are [1m[97min range[0m of it by being immediately 139[1m[97madjacent[0m to it. If there are no such targets, the unit ends its turn. Otherwise, the adjacent target 140with the [1m[97mfewest hit points[0m is selected; in a tie, the adjacent target with the fewest hit points 141which is first in reading order is selected. 142 143The unit deals damage equal to its [1m[97mattack power[0m to the selected target, reducing its hit points by 144that amount. If this reduces its hit points to 0 or fewer, the selected target [1m[97mdies[0m: its square 145becomes . and it takes no further turns. 146 147Each [1m[97munit[0m, either Goblin or Elf, has 3 [1m[97mattack power[0m and starts with 200 [1m[97mhit points[0m. 148 149For example, suppose the only Elf is about to attack: 150 151 HP: HP: 152G.... 9 G.... 9 153..G.. 4 ..G.. 4 154..E[1m[97mG[0m. 2 --> ..E.. 155..G.. 2 ..G.. 2 156...G. 1 ...G. 1 157 158The "HP" column shows the hit points of the Goblin to the left in the corresponding row. The Elf is 159in range of three targets: the Goblin above it (with 4 hit points), the Goblin to its right (with 2 160hit points), and the Goblin below it (also with 2 hit points). Because three targets are in range, 161the ones with the lowest hit points are selected: the two Goblins with 2 hit points each (one to the 162right of the Elf and one below the Elf). Of those, the Goblin first in reading order (the one to the 163right of the Elf) is selected. The selected Goblin's hit points (2) are reduced by the Elf's attack 164power (3), reducing its hit points to -1, killing it. 165 166After attacking, the unit's turn ends. Regardless of how the unit's turn ends, the next unit in the 167round takes its turn. If all units have taken turns in this round, the round ends, and a new round 168begins. 169 170The Elves look quite outnumbered. You need to determine the [1m[97moutcome[0m of the battle: the 171[1m[97mnumber of full rounds that were completed[0m (not counting the round in which combat ends) multiplied 172by [1m[97mthe sum of the hit points of all remaining units[0m at the moment combat ends. (Combat only ends 173when a unit finds no targets during its turn.) 174 175Below is an entire sample combat. Next to each map, each row's units' hit points are listed from 176left to right. 177 178Initially: 179####### 180#.G...# G(200) 181#...EG# E(200), G(200) 182#.#.#G# G(200) 183#..G#E# G(200), E(200) 184#.....# 185####### 186 187After 1 round: 188####### 189#..G..# G(200) 190#...EG# E(197), G(197) 191#.#G#G# G(200), G(197) 192#...#E# E(197) 193#.....# 194####### 195 196After 2 rounds: 197####### 198#...G.# G(200) 199#..GEG# G(200), E(188), G(194) 200#.#.#G# G(194) 201#...#E# E(194) 202#.....# 203####### 204 205Combat ensues; eventually, the top Elf dies: 206 207After 23 rounds: 208####### 209#...G.# G(200) 210#..G.G# G(200), G(131) 211#.#.#G# G(131) 212#...#E# E(131) 213#.....# 214####### 215 216After 24 rounds: 217####### 218#..G..# G(200) 219#...G.# G(131) 220#.#G#G# G(200), G(128) 221#...#E# E(128) 222#.....# 223####### 224 225After 25 rounds: 226####### 227#.G...# G(200) 228#..G..# G(131) 229#.#.#G# G(125) 230#..G#E# G(200), E(125) 231#.....# 232####### 233 234After 26 rounds: 235####### 236#G....# G(200) 237#.G...# G(131) 238#.#.#G# G(122) 239#...#E# E(122) 240#..G..# G(200) 241####### 242 243After 27 rounds: 244####### 245#G....# G(200) 246#.G...# G(131) 247#.#.#G# G(119) 248#...#E# E(119) 249#...G.# G(200) 250####### 251 252After 28 rounds: 253####### 254#G....# G(200) 255#.G...# G(131) 256#.#.#G# G(116) 257#...#E# E(113) 258#....G# G(200) 259####### 260 261More combat ensues; eventually, the bottom Elf dies: 262 263After 47 rounds: 264####### 265#G....# G(200) 266#.G...# G(131) 267#.#.#G# G(59) 268#...#.# 269#....G# G(200) 270####### 271 272Before the 48th round can finish, the top-left Goblin finds that there are no targets remaining, and 273so combat ends. So, the number of [1m[97mfull rounds[0m that were completed is [1m[97m47[0m, and the sum of the hit 274points of all remaining units is 200+131+59+200 = [1m[97m590[0m. From these, the [1m[97moutcome[0m of the battle is 47 * 275590 = [1m[97m27730[0m. 276 277Here are a few example summarized combats: 278 279####### ####### 280#G..#E# #...#E# E(200) 281#E#E.E# #E#...# E(197) 282#G.##.# --> #.E##.# E(185) 283#...#E# #E..#E# E(200), E(200) 284#...E.# #.....# 285####### ####### 286 287Combat ends after 37 full rounds 288Elves win with 982 total hit points left 289Outcome: 37 * 982 = [1m[97m36334[0m 290 291####### ####### 292#E..EG# #.E.E.# E(164), E(197) 293#.#G.E# #.#E..# E(200) 294#E.##E# --> #E.##.# E(98) 295#G..#.# #.E.#.# E(200) 296#..E#.# #...#.# 297####### ####### 298 299Combat ends after 46 full rounds 300Elves win with 859 total hit points left 301Outcome: 46 * 859 = [1m[97m39514[0m 302 303####### ####### 304#E.G#.# #G.G#.# G(200), G(98) 305#.#G..# #.#G..# G(200) 306#G.#.G# --> #..#..# 307#G..#.# #...#G# G(95) 308#...E.# #...G.# G(200) 309####### ####### 310 311Combat ends after 35 full rounds 312Goblins win with 793 total hit points left 313Outcome: 35 * 793 = [1m[97m27755[0m 314 315####### ####### 316#.E...# #.....# 317#.#..G# #.#G..# G(200) 318#.###.# --> #.###.# 319#E#G#G# #.#.#.# 320#...#G# #G.G#G# G(98), G(38), G(200) 321####### ####### 322 323Combat ends after 54 full rounds 324Goblins win with 536 total hit points left 325Outcome: 54 * 536 = [1m[97m28944[0m 326 327######### ######### 328#G......# #.G.....# G(137) 329#.E.#...# #G.G#...# G(200), G(200) 330#..##..G# #.G##...# G(200) 331#...##..# --> #...##..# 332#...#...# #.G.#...# G(200) 333#.G...G.# #.......# 334#.....G.# #.......# 335######### ######### 336 337Combat ends after 20 full rounds 338Goblins win with 937 total hit points left 339Outcome: 20 * 937 = [1m[97m18740[0m 340 341[1m[97mWhat is the outcome[0m of the combat described in your puzzle input? 342 343