aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

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 rounds; in each round, each unit that is still alive takes a turn, resolving all
     10of its actions before the next unit's turn begins. On each unit's turn, it tries to
     11move into range of an enemy (if it isn't already) and then attack (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 reading order: top-to-bottom, then left-to-right.  For instance, the order in which
     16units take their turns within a round is the reading order of their starting positions 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 targets (enemy units). If no targets remain,
     29combat ends.
     30
     31Then, the unit identifies all of the open squares (.) that are in range of each target; these are
     32the squares which are adjacent (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 already 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 move, but continues its turn with an
     38attack. Otherwise, since it is not in range of a target, it moves.
     39
     40To move, the unit first considers the squares that are in range and determines which of those
     41squares it could reach in the fewest steps. A step is a single movement to any adjacent (immediately
     42up, down, left, or right) open (.) square. Units cannot move into walls or other units. The unit
     43does this while considering the current positions of units and does not 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 tied for being reachable in the
     46fewest steps, the square which is first in reading order 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 in range (marked with a ? on the map).
     59
     60 - Of those squares, four are reachable (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 nearest, requiring the fewest steps (only 2) to reach
     64(marked !).
     65
     66 - Of those, the square which is first in reading order is chosen (+).
     67
     68
     69The unit then takes a single step toward the chosen square along the shortest path 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 more than one shortest
     72path 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...#       #4E212#       #..E..#
     77#...?.#  -->  #...!.#  -->  #...+.#  -->  #32101#  -->  #.....#
     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 right 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 attacks.
    137
    138To attack, the unit first determines all of the targets that are in range of it by being immediately
    139adjacent to it. If there are no such targets, the unit ends its turn. Otherwise, the adjacent target
    140with the fewest hit points 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 attack power to the selected target, reducing its hit points by
    144that amount. If this reduces its hit points to 0 or fewer, the selected target dies: its square
    145becomes . and it takes no further turns.
    146
    147Each unit, either Goblin or Elf, has 3 attack power and starts with 200 hit points.
    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..EG.  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 outcome of the battle: the
    171number of full rounds that were completed (not counting the round in which combat ends) multiplied
    172by the sum of the hit points of all remaining units 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 full rounds that were completed is 47, and the sum of the hit
    274points of all remaining units is 200+131+59+200 = 590. From these, the outcome of the battle is 47 *
    275590 = 27730.
    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 = 36334
    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 = 39514
    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 = 27755
    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 = 28944
    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 = 18740
    340
    341What is the outcome of the combat described in your puzzle input?
    342
    343