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 [1m[97mimmune system[0m and the [1m[97minfection[0m each have an army made up of several [1m[97mgroups[0m; each 10[1m[97mgroup[0m consists of one or more identical [1m[97munits[0m. The armies repeatedly [1m[97mfight[0m until only one army has 11units remaining. 12 13[1m[97mUnits[0m within a group all have the same [1m[97mhit points[0m (amount of damage a unit can take before it is 14destroyed), [1m[97mattack damage[0m (the amount of damage each unit deals), an [1m[97mattack type[0m, an 15[1m[97minitiative[0m (higher initiative units attack first and win ties), and sometimes 16[1m[97mweaknesses[0m or [1m[97mimmunities[0m. 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 [1m[97meffective power[0m: 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 [1m[97mfight[0m consists of two phases: [1m[97mtarget selection[0m and [1m[97mattacking[0m. 26 27During the [1m[97mtarget selection[0m 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 [1m[97mattacking[0m 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 [1m[97meffective power[0m to the defending group. However, if the defending group is 49[1m[97mimmune[0m to the attacking group's attack type, the defending group instead takes [1m[97mno damage[0m; if the 50defending group is [1m[97mweak[0m to the attacking group's attack type, the defending group instead takes 51[1m[97mdouble damage[0m. 52 53The defending group only loses [1m[97mwhole units[0m 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 = [1m[97m5216[0m units. 195 196You scan the reindeer's condition (your puzzle input); the white-bearded man looks nervous. As it 197stands now, [1m[97mhow many units would the winning army have[0m? 198 199