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 (8835B)


      1--- Day 24: Immune System Simulator 20XX ---
      2
      3After a weird buzzing noise, you appear back at the man's cottage. He seems relieved to see his
      4friend, but quickly notices that the little reindeer caught some kind of cold while out exploring.
      5
      6The portly man explains that this reindeer's immune system isn't similar to regular reindeer immune
      7systems:
      8
      9The immune system and the infection each have an army made up of several groups; each
     10group consists of one or more identical units.  The armies repeatedly fight until only one army has
     11units remaining.
     12
     13Units within a group all have the same hit points (amount of damage a unit can take before it is
     14destroyed), attack damage (the amount of damage each unit deals), an attack type, an
     15initiative (higher initiative units attack first and win ties), and sometimes
     16weaknesses or immunities. Here is an example group:
     17
     1818 units each with 729 hit points (weak to fire; immune to cold, slashing)
     19 with an attack that does 8 radiation damage at initiative 10
     20
     21Each group also has an effective power: the number of units in that group multiplied by their attack
     22damage. The above group has an effective power of 18 * 8 = 144. Groups never have zero or negative
     23units; instead, the group is removed from combat.
     24
     25Each fight consists of two phases: target selection and attacking.
     26
     27During the target selection phase, each group attempts to choose one target. In decreasing order of
     28effective power, groups choose their targets; in a tie, the group with the higher initiative chooses
     29first. The attacking group chooses to target the group in the enemy army to which it would deal the
     30most damage (after accounting for weaknesses and immunities, but not accounting for whether the
     31defending group has enough units to actually receive all of that damage).
     32
     33If an attacking group is considering two defending groups to which it would deal equal damage, it
     34chooses to target the defending group with the largest effective power; if there is still a tie, it
     35chooses the defending group with the highest initiative.  If it cannot deal any defending groups
     36damage, it does not choose a target.  Defending groups can only be chosen as a target by one
     37attacking group.
     38
     39At the end of the target selection phase, each group has selected zero or one groups to attack, and
     40each group is being attacked by zero or one groups.
     41
     42During the attacking phase, each group deals damage to the target it selected, if any. Groups attack
     43in decreasing order of initiative, regardless of whether they are part of the infection or the
     44immune system. (If a group contains no units, it cannot attack.)
     45
     46The damage an attacking group deals to a defending group depends on the attacking group's attack
     47type and the defending group's immunities and weaknesses.  By default, an attacking group would deal
     48damage equal to its effective power to the defending group.  However, if the defending group is
     49immune to the attacking group's attack type, the defending group instead takes no damage; if the
     50defending group is weak to the attacking group's attack type, the defending group instead takes
     51double damage.
     52
     53The defending group only loses whole units from damage; damage is always dealt in such a way that it
     54kills the most units possible, and any remaining damage to a unit that does not immediately kill it
     55is ignored. For example, if a defending group contains 10 units with 10 hit points each and receives
     5675 damage, it loses exactly 7 units and is left with 3 units at full health.
     57
     58After the fight is over, if both armies still contain units, a new fight begins; combat only ends
     59once one army has lost all of its units.
     60
     61For example, consider the following armies:
     62
     63Immune System:
     6417 units each with 5390 hit points (weak to radiation, bludgeoning) with
     65 an attack that does 4507 fire damage at initiative 2
     66989 units each with 1274 hit points (immune to fire; weak to bludgeoning,
     67 slashing) with an attack that does 25 slashing damage at initiative 3
     68
     69Infection:
     70801 units each with 4706 hit points (weak to radiation) with an attack
     71 that does 116 bludgeoning damage at initiative 1
     724485 units each with 2961 hit points (immune to radiation; weak to fire,
     73 cold) with an attack that does 12 slashing damage at initiative 4
     74
     75If these armies were to enter combat, the following fights, including details during the target
     76selection and attacking phases, would take place:
     77
     78Immune System:
     79Group 1 contains 17 units
     80Group 2 contains 989 units
     81Infection:
     82Group 1 contains 801 units
     83Group 2 contains 4485 units
     84
     85Infection group 1 would deal defending group 1 185832 damage
     86Infection group 1 would deal defending group 2 185832 damage
     87Infection group 2 would deal defending group 2 107640 damage
     88Immune System group 1 would deal defending group 1 76619 damage
     89Immune System group 1 would deal defending group 2 153238 damage
     90Immune System group 2 would deal defending group 1 24725 damage
     91
     92Infection group 2 attacks defending group 2, killing 84 units
     93Immune System group 2 attacks defending group 1, killing 4 units
     94Immune System group 1 attacks defending group 2, killing 51 units
     95Infection group 1 attacks defending group 1, killing 17 units
     96
     97Immune System:
     98Group 2 contains 905 units
     99Infection:
    100Group 1 contains 797 units
    101Group 2 contains 4434 units
    102
    103Infection group 1 would deal defending group 2 184904 damage
    104Immune System group 2 would deal defending group 1 22625 damage
    105Immune System group 2 would deal defending group 2 22625 damage
    106
    107Immune System group 2 attacks defending group 1, killing 4 units
    108Infection group 1 attacks defending group 2, killing 144 units
    109
    110Immune System:
    111Group 2 contains 761 units
    112Infection:
    113Group 1 contains 793 units
    114Group 2 contains 4434 units
    115
    116Infection group 1 would deal defending group 2 183976 damage
    117Immune System group 2 would deal defending group 1 19025 damage
    118Immune System group 2 would deal defending group 2 19025 damage
    119
    120Immune System group 2 attacks defending group 1, killing 4 units
    121Infection group 1 attacks defending group 2, killing 143 units
    122
    123Immune System:
    124Group 2 contains 618 units
    125Infection:
    126Group 1 contains 789 units
    127Group 2 contains 4434 units
    128
    129Infection group 1 would deal defending group 2 183048 damage
    130Immune System group 2 would deal defending group 1 15450 damage
    131Immune System group 2 would deal defending group 2 15450 damage
    132
    133Immune System group 2 attacks defending group 1, killing 3 units
    134Infection group 1 attacks defending group 2, killing 143 units
    135
    136Immune System:
    137Group 2 contains 475 units
    138Infection:
    139Group 1 contains 786 units
    140Group 2 contains 4434 units
    141
    142Infection group 1 would deal defending group 2 182352 damage
    143Immune System group 2 would deal defending group 1 11875 damage
    144Immune System group 2 would deal defending group 2 11875 damage
    145
    146Immune System group 2 attacks defending group 1, killing 2 units
    147Infection group 1 attacks defending group 2, killing 142 units
    148
    149Immune System:
    150Group 2 contains 333 units
    151Infection:
    152Group 1 contains 784 units
    153Group 2 contains 4434 units
    154
    155Infection group 1 would deal defending group 2 181888 damage
    156Immune System group 2 would deal defending group 1 8325 damage
    157Immune System group 2 would deal defending group 2 8325 damage
    158
    159Immune System group 2 attacks defending group 1, killing 1 unit
    160Infection group 1 attacks defending group 2, killing 142 units
    161
    162Immune System:
    163Group 2 contains 191 units
    164Infection:
    165Group 1 contains 783 units
    166Group 2 contains 4434 units
    167
    168Infection group 1 would deal defending group 2 181656 damage
    169Immune System group 2 would deal defending group 1 4775 damage
    170Immune System group 2 would deal defending group 2 4775 damage
    171
    172Immune System group 2 attacks defending group 1, killing 1 unit
    173Infection group 1 attacks defending group 2, killing 142 units
    174
    175Immune System:
    176Group 2 contains 49 units
    177Infection:
    178Group 1 contains 782 units
    179Group 2 contains 4434 units
    180
    181Infection group 1 would deal defending group 2 181424 damage
    182Immune System group 2 would deal defending group 1 1225 damage
    183Immune System group 2 would deal defending group 2 1225 damage
    184
    185Immune System group 2 attacks defending group 1, killing 0 units
    186Infection group 1 attacks defending group 2, killing 49 units
    187
    188Immune System:
    189No groups remain.
    190Infection:
    191Group 1 contains 782 units
    192Group 2 contains 4434 units
    193
    194In the example above, the winning army ends up with 782 + 4434 = 5216 units.
    195
    196You scan the reindeer's condition (your puzzle input); the white-bearded man looks nervous.  As it
    197stands now, how many units would the winning army have?
    198
    199