-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15.js
353 lines (329 loc) · 9.75 KB
/
15.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
const eol = require('os').EOL
const log = (message, level) => {
// if (level) {
// console.log(message)
// }
}
const getBasicMap = () => {
const map = []
require('fs')
// .readFileSync(`inputData/15-map-18740.txt`, `ascii`)
// .readFileSync(`inputData/15-map-27730.txt`, `ascii`)
// .readFileSync(`inputData/15-map-27755.txt`, `ascii`)
// .readFileSync(`inputData/15-map-28944.txt`, `ascii`)
// .readFileSync(`inputData/15-map-36334.txt`, `ascii`)
// .readFileSync(`inputData/15-map-39514.txt`, `ascii`)
.readFileSync(`inputData/15-map-real.txt`, `ascii`)
.split(eol)
.forEach(line => {
map.push(
line.split('').map((glyph, x) => {
const node = {
key: `${x}x${map.length}`,
x: x,
y: map.length,
initialGlyph: glyph,
floor: glyph === '#' ? glyph : '.',
goblin: glyph === 'G',
elf: glyph === 'E',
getUnit: () => node.elf || node.goblin,
isOpen: () => !node.getUnit() && node.floor === '.',
}
return node
})
)
})
return map
}
const createUnits = map => {
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[y].length; x++) {
if (map[y][x].elf) {
map[y][x].elf = createUnit(map[y][x])
} else if (map[y][x].goblin) {
map[y][x].goblin = createUnit(map[y][x])
}
}
}
}
const getNeighborNodes = node => {
// no diagonals, reading order
const { x, y } = node
return [
getNode(x, y - 1),
getNode(x - 1, y),
getNode(x + 1, y),
getNode(x, y + 1),
]
}
const getNode = (x, y) => {
if (y >= map.length || y < 0 || x >= map[y].length || x < 0) {
return null
}
return map[y][x]
}
const createUnit = node => {
const unit = {
team: node.elf ? 'Elves' : 'Goblins',
glyph: node.elf ? 'E' : 'G',
node: node,
hp: 200,
attackPower: 3,
getTargets: () => {
return unit.glyph === 'E' ? units.goblins : units.elves
},
getTargetTilesInRange: range => {
return (range || unit.getTilesInRange()).filter(n => {
return n !== null && n.getUnit() && n.getUnit().glyph != unit.glyph
})
},
getOpenTilesInRange: range => {
return (range || unit.getTilesInRange()).filter(t => {
return t !== null && t.isOpen()
})
},
getTilesInRange: () => {
return getNeighborNodes(unit.node)
},
lastAction: null,
takeTurn: round => {
log(
`==============================================> ${unit.glyph} at ${
unit.node.key
} goes next`
)
if (unit.lastAction === round) {
log(`jk, already went!`)
return
}
unit.lastAction = round
let unitRange = unit.getTilesInRange()
unit.move(unitRange)
unitRange = unit.getTilesInRange()
unit.attack(unitRange)
},
move: unitRange => {
// identify targets
const targets = unit.getTargets()
log(`Got ${targets.length} targets`)
// if no targets left, game over
if (!targets.length) {
log(
`No more targets! ${unit.team} won after ${roundCount} full rounds`,
1
)
drawMap(map)
process.exit()
}
let targetsInRange = unit.getTargetTilesInRange(unitRange)
if (targetsInRange.length) {
return
}
let destinationTiles = []
targets.forEach(t => {
destinationTiles = destinationTiles.concat(t.getOpenTilesInRange())
})
log(`${destinationTiles.length} tiles we want to move to`)
const move = unit.getMove(destinationTiles, unitRange)
if (!move) {
log(`${unit.glyph} at ${unit.node.key} is not moving`)
return
}
const moveFromNode = unit.node
move.elf = moveFromNode.elf
move.goblin = moveFromNode.goblin
moveFromNode.elf = false
moveFromNode.goblin = false
unit.node = move
log(`Moved ${unit.glyph} from ${moveFromNode.key} to ${unit.node.key}`)
},
attack: unitRange => {
let targetsInRange = unit.getTargetTilesInRange(unitRange)
if (targetsInRange.length) {
const target = unit.selectAttackTarget(targetsInRange)
target.hp -= unit.attackPower
log(
`${unit.node.key} attacks ${target.glyph} unit: ${
target.node.key
}, down to ${target.hp}hp`
)
if (target.hp <= 0) {
log(`Burying ${target.glyph} at ${target.node.key}`)
bury(target)
}
}
},
selectAttackTarget: targetsInRange => {
// in a tie, the adjacent target with the fewest hit points which is first in reading order is selected.
let minHp = Infinity
let target = null
targetsInRange.forEach(t => {
if (t.getUnit().hp < minHp) {
target = t.getUnit()
minHp = t.getUnit().hp
}
})
return target
},
getMove: (destinationTiles, unitRange) => {
const myAvailableMoves = unit.getOpenTilesInRange(unitRange)
log(
`${myAvailableMoves.length} moves available to ${unit.glyph} at ${
unit.node.key
}`
)
// Is there a move I can make that will immediately put me in range of a target?
let move = null
// for (let i = 0; i < myAvailableMoves.length; i++) {
// const inRangeForMovement = destinationTiles.filter(
// dt => dt.key === myAvailableMoves[i].key
// )
// if (inRangeForMovement.length) {
// move = inRangeForMovement[0]
// log(`Found a move that is in range of a target: ${move.key}!!`)
// console.log(destinationTiles)
// return move
// }
// }
// Find the best move that wil put me closer to getting in range of an attacker
// go through the destination tiles, find the closest, make sure it is navigable
let min = Infinity
myAvailableMoves.forEach(start => {
const bestPaths = unit.getAllPaths(start)
log(
`********************************** Getting all paths from ${
start.key
}`
)
if (!Object.keys(bestPaths).length) {
log(`No path???`)
console.log(Object.keys(bestPaths).length)
}
destinationTiles.forEach(dt => {
// TODO - do from each step in case 1 is better than another
log(`Getting best path to ${dt.key}`)
const path = unit.getBestPath(start, dt, bestPaths)
if (path.length && min > path.length) {
log(
`New min: ${path.length} steps from ${start.key} to ${
dt.key
} starts at ${start.key}`
)
min = path.length
move = start
}
})
})
return move
},
getBestPath: (start, end, allPaths) => {
const path = [start.key]
if (start.key === end.key) {
log(`Okay, well that was easy - start was the end`)
return path
}
let currentKey = end.key
while (currentKey != start.key) {
log(`Get best path, from ${start.key} -> ${currentKey}`)
if (!allPaths[currentKey]) {
log(`You can't get there from here`)
return []
}
path.push(allPaths[currentKey])
currentKey = allPaths[currentKey].key
}
return path.reverse()
},
getAllPaths: start => {
const unexplored = [start]
bestPaths = {}
bestPaths[start.key] = false
while (unexplored.length) {
log(`Node ${start.key} unexplored: ${unexplored.length}`)
current = unexplored.shift()
const currentNeighbors = getNeighborNodes(current).filter(
n => n != null && n.isOpen()
)
// if (current.key === '5x5') {
// //log(`-----------------------------------Ok...getting neighbords`)
// //log(getNeighborNodes(current))
// }
log(`Node ${current.key} currentNeighbors: ${currentNeighbors.length}`)
currentNeighbors.forEach(next => {
if (!bestPaths[next.key]) {
log(`Next ${next.key} is unexplored, push ${current.key}`)
unexplored.push(next)
bestPaths[next.key] = current
}
})
}
return bestPaths
},
}
if (node.elf) {
units.elves.push(unit)
} else {
units.goblins.push(unit)
}
return unit
}
const bury = unit => {
unit.node.elf = false
unit.node.goblin = false
units.elves = units.elves.filter(u => u.node.key !== unit.node.key)
units.goblins = units.goblins.filter(u => u.node.key !== unit.node.key)
}
const drawMap = map => {
process.stdout.write('\033c')
console.log()
console.log(`After round: ${roundCount}`)
let sum = 0
for (let y = 0; y < map.length; y++) {
const unitHps = []
for (let x = 0; x < map[y].length; x++) {
const unit = map[y][x].getUnit()
if (unit) {
process.stdout.write(unit.glyph)
unitHps.push(`${unit.glyph}(${unit.hp})`)
sum += unit.hp
} else {
process.stdout.write(map[y][x].floor)
}
}
if (unitHps.length) {
process.stdout.write(` ${unitHps.join(', ')}`)
}
process.stdout.write(eol)
}
console.log(`result: ${sum} * ${roundCount}=${sum * roundCount}`)
console.log()
}
let roundCount = 0
const round = map => {
// reading order
const height = map.length
const width = map[0].length
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const unit = map[y][x].getUnit()
if (unit) {
unit.takeTurn(roundCount)
// drawMap(map)
}
}
}
roundCount++
}
const map = getBasicMap()
const units = {
goblins: [],
elves: [],
}
createUnits(map)
drawMap(map)
setInterval(() => {
round(map)
drawMap(map)
}, 1000)
// 209836 is too low for real
// 222912 is too high for real