-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadv24.js
287 lines (227 loc) · 7.5 KB
/
adv24.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env node
"use strict";
const fs = require('fs');
const path = require('path');
const contents = fs.readFileSync(
path.basename(__filename, ".js") + ".txt", 'utf8')
.trim();
const groupRe = /(\d+) units each with (\d+) hit points \(?([^\)]*)\)? ?with an attack that does (\d+) (\w+) damage at initiative (\d+)/
function Group(id, units, hp, buffs, attack, attackType, initiative) {
// console.log(units, hp, buffs, attack, attackType, initiative)
this.id = id
this.units = units
this.hp = hp
this.attack = attack
this.attackType = attackType
this.initiative = initiative
this.buffs = {}
for (const key of ["immune", "weak"]) {
this.buffs[key] = new Set()
if (buffs[key] == undefined)
continue
for (const buff of buffs[key]) {
this.buffs[key].add(buff)
}
}
}
Group.prototype.clone = function () {
return new Group(this.id, this.units, this.hp, this.buffs, this.attack, this.attackType, this.initiative)
}
Group.prototype.effectivePower = function () {
return this.units * this.attack
}
Group.prototype.attackMultiplierAgainst = function (targetGroup) {
let attackMultiplier = 1
if (targetGroup.buffs.immune.has(this.attackType)) {
attackMultiplier = 0
} else if (targetGroup.buffs.weak.has(this.attackType)) {
attackMultiplier = 2
}
return attackMultiplier
}
Group.prototype.selectTarget = function (targetGroups) {
let group = this
const uep = this.effectivePower()
let damages = targetGroups.map(target => group.attackMultiplierAgainst(target) * uep)
let maxDamage = Math.max(...damages)
if (maxDamage === 0)
return
for (let i in damages) {
const damage = damages[i]
if (this.verbose)
console.log(`${this.army.name} group ${this.id} would deal defending group ${targetGroups[i].id} ${damage} damage`)
}
let targets = targetGroups.filter((group, i) => damages[i] == maxDamage)
if (targets.length === 1)
return targets[0]
let ep = targets.map(target => target.effectivePower())
const maxEP = Math.max(...ep)
targets = targets.filter((group, i) => ep[i] == maxEP)
if (targets.length === 1)
return targets[0]
targets.sort((b, a) => a.initiative - b.initiative)
return targets[0]
}
Group.prototype.attackTarget = function (target) {
let multiplier = this.attackMultiplierAgainst(target)
let killed = Math.floor(multiplier * this.effectivePower() / target.hp)
target.units = Math.max(0, target.units - killed)
if (this.verbose)
console.log(`${this.army.name} group ${this.id} attacks defending group ${target.id}, killing ${killed}`)
}
Group.prototype.toString = function () {
return `Group ${this.id} contains ${this.units} units`
}
function Army(name, groups) {
this.name = name
this.groups = groups
this.groups.sort((b, a) => a.initiative - b.initiative)
for (let group of groups) {
group.army = this
}
}
Army.prototype.selectTarget = function (targetArmy) {
let groupsTargets = []
let targets = targetArmy.groups
let groups = this.groups.slice()
groups.sort((b, a) => a.effectivePower() - b.effectivePower())
for (const group of groups) {
const target = group.selectTarget(targets)
if (target == null)
continue
targets = targets.filter(_target => _target !== target)
groupsTargets.push([group, target])
}
return groupsTargets
}
Army.prototype.toString = function () {
let groupsString = this.groups.map(group => group.toString()).join("\n")
if (this.groups.length < 1)
groupsString = "No groups remain"
return this.name + "\n" +
groupsString +
"\n"
}
Army.prototype.boost = function (boost) {
for (let group of this.groups)
group.attack += boost
return this
}
Army.prototype.isAlive = function () {
this.groups = this.groups.filter(group => group.units > 0)
if (this.groups.length > 0)
return true
return false
}
Army.prototype.setVerbose = function (verbose) {
this.verbose = verbose
for (const group of this.groups)
group.verbose = verbose
}
Army.prototype.score = function () {
return this.groups.reduce((a, group) => a + group.units, 0)
}
Army.prototype.clone = function () {
return new Army(this.name, this.groups.map(group => group.clone()))
}
function Battlefield(army1, army2) {
this.armies = [army1, army2]
}
Battlefield.prototype.setVerbose = function (verbose) {
this.verbose = verbose
for (const army of this.armies)
army.setVerbose(verbose)
return this
}
Battlefield.prototype.score = function () {
return this.armies.reduce((score, army) => score + army.score(), 0)
}
Battlefield.prototype.turn = function () {
let groupsTargets = []
if (this.verbose)
console.log(this.toString())
for (let army = 0; army < this.armies.length; army++) {
let target = (army + 1) % 2
groupsTargets = groupsTargets.concat(this.armies[army].selectTarget(this.armies[target]))
}
groupsTargets = groupsTargets.sort((b, a) => a[0].initiative - b[0].initiative)
for (const groupTarget of groupsTargets) {
groupTarget[0].attackTarget(groupTarget[1])
}
if (this.verbose)
console.log()
for (let army = 0; army < this.armies.length; army++) {
if (!this.armies[army].isAlive()) {
if (this.verbose)
console.log(this.toString())
return this.armies[(army + 1) % 2]
}
}
return
}
Battlefield.prototype.toString = function () {
let res = new String()
for (let army of this.armies)
res += army.toString()
return res
}
Battlefield.prototype.clone = function () {
return new Battlefield(...this.armies.map(army => army.clone()))
}
const battlefield = new Battlefield(...contents.split("\n\n").map(teamString => {
let lines = teamString.split("\n")
const teamName = lines.shift().split(":")[0]
let groups = []
for (const groupLine of lines) {
const match = groupLine.match(groupRe)
if (!match)
throw (`Failed to parse line "${groupLine}"`)
let buffs = {}
if (match[3]) {
for (const buffStr of match[3].split("; ")) {
const temp = buffStr.split(" to ")
buffs[temp[0]] = temp[1].split(", ")
}
}
groups.push(new Group(groups.length + 1, parseInt(match[1]), parseInt(match[2]), buffs, parseInt(match[4]), match[5], parseInt(match[6])))
}
return new Army(teamName, groups)
}))
function strategyUsualBattle(battlefield) {
let winner
let score
while (!winner) {
winner = battlefield.turn()
let newArmiesScore = battlefield.armies.map(army => army.score())
let newScore = battlefield.score()
if (score === newScore)
return
score = newScore
}
return winner
}
const IMMUNE_SYSTEM = 'Immune System'
function strategyImmuneBoost(battlefield) {
let armies = battlefield.armies
let immuneSystemI = 0
while (armies[immuneSystemI].name !== IMMUNE_SYSTEM)
immuneSystemI++
let immuneSystem = battlefield.armies[immuneSystemI]
let infection = battlefield.armies[(immuneSystemI + 1) % 2]
let winner = infection
let boost = 1
for (; winner.name != IMMUNE_SYSTEM; boost += 10) {
// console.log(boost, immuneSystemBoosted)
winner = strategyUsualBattle(new Battlefield(infection.clone(), immuneSystem.clone().boost(boost)))
}
let immune_lost = false
for (; !immune_lost; boost--) {
let _winner = strategyUsualBattle(new Battlefield(infection.clone(), immuneSystem.clone().boost(boost)).setVerbose(false))
immune_lost = _winner == null || _winner.name != IMMUNE_SYSTEM
if (!immune_lost)
winner = _winner
}
return winner
}
console.log("Part one:", strategyUsualBattle(battlefield.clone()).score())
console.log("Part two:", strategyImmuneBoost(battlefield).score())