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