|
| 1 | +const PRINT_COMBAT_TEXT = false; |
| 2 | + |
| 3 | +function print () { |
| 4 | + if (PRINT_COMBAT_TEXT) { |
| 5 | + const args = Array.from(arguments); |
| 6 | + |
| 7 | + console.log(...args); |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +const parseArmyInformation = (input) => { |
| 12 | + return input |
| 13 | + .split('\n\n') |
| 14 | + .map((army, armyIndex) => { |
| 15 | + const armyName = armyIndex === 0 ? 'Immune System' : 'Infection'; |
| 16 | + const groups = army |
| 17 | + .split('\n') |
| 18 | + .slice(1) |
| 19 | + .map((line, groupIndex) => { |
| 20 | + const parts = /(\d+) units each with (\d+) hit points .*with an attack that does (\d+) (\w+) damage at initiative (\d+)/.exec(line.trim()); |
| 21 | + const modifiers = /\((.*)\)/.exec(line.trim()); |
| 22 | + |
| 23 | + const tags = modifiers |
| 24 | + ? modifiers[1].split(';').reduce((map, definition) => { |
| 25 | + /weak to/.test(definition) |
| 26 | + ? map.set('weaknesses', new Set(/weak to (.*)/.exec(definition)[1].split(', '))) |
| 27 | + : map.set('immunities', new Set(/immune to (.*)/.exec(definition)[1].split(', '))); |
| 28 | + |
| 29 | + return map; |
| 30 | + }, new Map()) |
| 31 | + : new Map(); |
| 32 | + |
| 33 | + return { |
| 34 | + armyName, |
| 35 | + number: groupIndex + 1, |
| 36 | + units: +parts[1], |
| 37 | + hitPoints: +parts[2], |
| 38 | + weaknesses: tags.get('weaknesses') || new Set(), |
| 39 | + immunities: tags.get('immunities') || new Set(), |
| 40 | + attackDamage: +parts[3], |
| 41 | + attackType: parts[4], |
| 42 | + initiative: +parts[5], |
| 43 | + get effectivePower() { |
| 44 | + return this.units * this.attackDamage; |
| 45 | + }, |
| 46 | + get isDead() { |
| 47 | + return this.units <= 0; |
| 48 | + }, |
| 49 | + }; |
| 50 | + }); |
| 51 | + |
| 52 | + return { |
| 53 | + name: armyName, |
| 54 | + groups, |
| 55 | + get isDead() { |
| 56 | + return this.groups.every((group) => group.isDead); |
| 57 | + }, |
| 58 | + }; |
| 59 | + }); |
| 60 | +}; |
| 61 | + |
| 62 | +const printArmyStatus = (army) => { |
| 63 | + print(`${army.name}:`); |
| 64 | + army.groups.every((group) => group.isDead) |
| 65 | + ? print('No groups remain.') |
| 66 | + : army.groups.forEach((group) => !group.isDead && print(`Group ${group.number} contains ${group.units} units`)); |
| 67 | +}; |
| 68 | + |
| 69 | +const attackerSelectionSort = (a, b) => { |
| 70 | + return b.effectivePower - a.effectivePower === 0 |
| 71 | + ? b.initiative - a.initiative |
| 72 | + : b.effectivePower - a.effectivePower; |
| 73 | +}; |
| 74 | + |
| 75 | +const effectiveDamage = (attacker, defender) => { |
| 76 | + const { effectivePower, attackType } = attacker; |
| 77 | + |
| 78 | + return (defender.weaknesses.has(attackType) ? 2 : (defender.immunities.has(attackType) ? 0 : 1)) * effectivePower; |
| 79 | +}; |
| 80 | + |
| 81 | +const targetSelectionSort = (a, b) => { |
| 82 | + return b.damage - a.damage === 0 |
| 83 | + ? b.group.effectivePower - a.group.effectivePower === 0 |
| 84 | + ? b.group.initiative - a.group.initiative |
| 85 | + : b.group.effectivePower - a.group.effectivePower |
| 86 | + : b.damage - a.damage; |
| 87 | +}; |
| 88 | + |
| 89 | +const targetSelectionPhase = (attackers, defenders) => { |
| 90 | + const sortedAttackers = [...attackers.groups].filter((group) => !group.isDead).sort(attackerSelectionSort); |
| 91 | + const availableDefenders = [...defenders.groups].filter((group) => !group.isDead); |
| 92 | + const plannedAttacks = []; |
| 93 | + |
| 94 | + sortedAttackers.forEach((attackingGroup) => { |
| 95 | + const effectiveDamageOnDefenders = availableDefenders.map((defendingGroup) => { |
| 96 | + return { |
| 97 | + damage: effectiveDamage(attackingGroup, defendingGroup), |
| 98 | + group: defendingGroup, |
| 99 | + }; |
| 100 | + }); |
| 101 | + |
| 102 | + // print target selection phase |
| 103 | + effectiveDamageOnDefenders.forEach(({ damage, group }) => { |
| 104 | + print(`${attackers.name} group ${attackingGroup.number} would deal defending group ${group.number} ${damage} damage`); |
| 105 | + }); |
| 106 | + |
| 107 | + if (!effectiveDamageOnDefenders.length) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + const selectedTarget = effectiveDamageOnDefenders.sort(targetSelectionSort)[0]; |
| 112 | + |
| 113 | + if (selectedTarget.damage === 0) { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + plannedAttacks.push({ |
| 118 | + attackingGroup, |
| 119 | + defendingGroup: selectedTarget.group, |
| 120 | + }); |
| 121 | + |
| 122 | + const selectedDefenderIndex = availableDefenders.findIndex(({ number }) => selectedTarget.group.number === number); |
| 123 | + |
| 124 | + availableDefenders.splice(selectedDefenderIndex, 1); |
| 125 | + }); |
| 126 | + |
| 127 | + return plannedAttacks; |
| 128 | +}; |
| 129 | + |
| 130 | +const attackingPhaseSort = (a, b) => { |
| 131 | + return b.attackingGroup.initiative - a.attackingGroup.initiative; |
| 132 | +}; |
| 133 | + |
| 134 | +const attackingPhase = (plannedAttacks) => { |
| 135 | + const sortedAttackingOrder = plannedAttacks.sort(attackingPhaseSort); |
| 136 | + |
| 137 | + for (let i = 0; i < sortedAttackingOrder.length; i++) { |
| 138 | + const { attackingGroup, defendingGroup } = sortedAttackingOrder[i]; |
| 139 | + const damage = effectiveDamage(attackingGroup, defendingGroup); |
| 140 | + const killedUnits = Math.min(Math.floor(damage / defendingGroup.hitPoints), defendingGroup.units); |
| 141 | + |
| 142 | + print(`${attackingGroup.armyName} group ${attackingGroup.number} attacks defending group ${defendingGroup.number}, killing ${killedUnits} units`); |
| 143 | + |
| 144 | + defendingGroup.units -= killedUnits; |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +const simulate = (armies) => { |
| 149 | + while (!armies[0].isDead && !armies[1].isDead) { |
| 150 | + printArmyStatus(armies[0]); |
| 151 | + printArmyStatus(armies[1]); |
| 152 | + |
| 153 | + print(''); |
| 154 | + |
| 155 | + // target selection phase |
| 156 | + const plannedAttacks = [ |
| 157 | + ...targetSelectionPhase(armies[1], armies[0]), |
| 158 | + ...targetSelectionPhase(armies[0], armies[1]), |
| 159 | + ]; |
| 160 | + |
| 161 | + // halt if opposing groups are immune to each other |
| 162 | + if (!plannedAttacks.length) { |
| 163 | + return [false, 0]; |
| 164 | + } |
| 165 | + |
| 166 | + print(''); |
| 167 | + |
| 168 | + // attacking phase |
| 169 | + attackingPhase(plannedAttacks); |
| 170 | + |
| 171 | + print(''); |
| 172 | + } |
| 173 | + |
| 174 | + printArmyStatus(armies[0]); |
| 175 | + printArmyStatus(armies[1]); |
| 176 | + |
| 177 | + return [ |
| 178 | + !armies[0].isDead, |
| 179 | + armies[0].groups.reduce((units, group) => units + group.units, 0), |
| 180 | + ]; |
| 181 | +}; |
| 182 | + |
| 183 | +const boost = (army, amount) => { |
| 184 | + army.groups.forEach((group) => group.attackDamage += amount); |
| 185 | +}; |
| 186 | + |
| 187 | +module.exports = (input) => { |
| 188 | + let appliedBoost = 1; |
| 189 | + let minimumBoost = appliedBoost; |
| 190 | + let maximumBoost = Infinity; |
| 191 | + |
| 192 | + while (appliedBoost <= maximumBoost) { |
| 193 | + const armies = parseArmyInformation(input); |
| 194 | + |
| 195 | + boost(armies[0], appliedBoost); |
| 196 | + |
| 197 | + const [success, immuneSystemUnitsLeft] = simulate(armies); |
| 198 | + |
| 199 | + if (!success) { |
| 200 | + minimumBoost = appliedBoost; |
| 201 | + appliedBoost = maximumBoost === Infinity |
| 202 | + ? appliedBoost *= 2 |
| 203 | + : Math.floor((maximumBoost - minimumBoost) / 2 + 1) + minimumBoost; |
| 204 | + } else if (appliedBoost < maximumBoost) { |
| 205 | + maximumBoost = appliedBoost; |
| 206 | + appliedBoost = Math.floor((maximumBoost - minimumBoost) / 2 + 1) + minimumBoost; |
| 207 | + } else { |
| 208 | + return immuneSystemUnitsLeft; |
| 209 | + } |
| 210 | + } |
| 211 | +}; |
0 commit comments