-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb.js
156 lines (134 loc) · 4.34 KB
/
b.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
const fs = require('fs');
const enemy = { G: 'E', E: 'G' };
function getUnitList(map) {
const list = [];
for (let x = 0; x < map.length; x++) {
for (let y = 0; y < map[x].length; y++) {
if (map[x][y].unit) {
list.push({ x, y, unit: map[x][y].unit, hit: map[x][y].hit });
}
}
}
return list;
}
function getNeighbors(pos) {
return [
{ x: pos.x - 1, y: pos.y },
{ x: pos.x, y: pos.y - 1 },
{ x: pos.x, y: pos.y + 1 },
{ x: pos.x + 1, y: pos.y },
];
}
function isReadyToAttack(map, unit) {
const attackRange = getNeighbors(unit)
.filter((coo) => map[coo.x][coo.y].unit == enemy[unit.unit])
.reduce((next, curr) => {
if (map[curr.x][curr.y].hit < next.hit) {
return { hit: map[curr.x][curr.y].hit, x: curr.x, y: curr.y };
}
return next;
}, { hit: Infinity });
if (attackRange.hit === Infinity) {
return null;
}
return attackRange;
}
function attack(map, attacker, attacked, units, hits) {
// console.log('ATTACK', attacker, attacked)
map[attacked.x][attacked.y].hit -= hits[attacker.unit];
if (map[attacked.x][attacked.y].hit < 0) {
map[attacked.x][attacked.y] = '.';
const attackedIndex = units.filter(Boolean).findIndex((u) => u.x === attacked.x && u.y === attacked.y);
units[attackedIndex] = null;
}
return true;
}
function tryAttack(map, unit, units, hits) {
const nextAttack = isReadyToAttack(map, unit);
return nextAttack ? attack(map, unit, nextAttack, units, hits) : false;
}
function findMove(map, targetUnit, positions, depth) {
let nextPositions = [];
positions.filter((pos) => map[pos.x][pos.y] === '.').forEach((pos) => {
map[pos.x][pos.y] = depth;
const newPositions = getNeighbors(pos)
.filter((pos) => !nextPositions.find((item) => item.x === pos.x && item.y === pos.y));
nextPositions.push(...newPositions);
});
if (nextPositions.some((pos) => map[pos.x][pos.y].unit === targetUnit)) {
return depth;
} else if (nextPositions.length === 0) {
return Infinity;
}
return findMove(map, targetUnit, nextPositions, depth + 1);
}
function getNextMove(map, unit) {
const targetUnit = enemy[unit.unit];
const nextMove = getNeighbors(unit)
.reduce((next, curr) => {
const mapCopy = map.map((row) => row.slice());
mapCopy[unit.x][unit.y] = '.';
const distance = findMove(mapCopy, targetUnit, [curr], 1);
if (distance < next.distance) {
return { distance, x: curr.x, y: curr.y };
}
return next;
}, { distance: Infinity });
return nextMove.distance === Infinity ? null : nextMove;
}
function tryMove(map, unit) {
const nextMove = getNextMove(map, unit);
if (nextMove) {
map[nextMove.x][nextMove.y] = map[unit.x][unit.y];
map[unit.x][unit.y] = '.';
unit.x = nextMove.x;
unit.y = nextMove.y;
return true;
}
return false;
}
function fight(map, hits) {
while(true) {
const units = getUnitList(map);
for (let unit of units) {
if (!unit) {
continue;
}
if (!tryAttack(map, unit, units, hits)) {
if (tryMove(map, unit)) {
tryAttack(map, unit, units, hits);
}
}
map[unit.x][unit.y].rounds++;
const lastRound = units
.filter(Boolean)
.map((item) => item.unit)
.reduce((found, current) => found === current ? found : false);
if (lastRound) {
const roundCount = Math.min(...units.filter(Boolean).map((u) => map[u.x][u.y].rounds));
const hitSum = getUnitList(map).reduce((sum, curr) => sum + curr.hit, 0);
console.log(roundCount, hitSum, getUnitList(map));
return roundCount * hitSum;
}
}
}
}
const map = fs
.readFileSync('input.txt', 'utf-8')
.split('\n')
.map((line) => line.split('').map((unit) => {
return (unit === '#' || unit === '.') ? unit : { unit, hit: 200, rounds: 0 };
}));
const elfCount = getUnitList(map).filter((unit) => unit.unit === 'E').length;
let attackStrength = 3;
do {
attackStrength++;
const mapCopy = map.map((row) => row.map((item) => typeof item === 'string' ? item : { unit: item.unit, hit: 200, rounds: 0 }));
console.log('Check attack', attackStrength)
const result = fight(mapCopy, { G: 3, E: attackStrength });
const units = getUnitList(mapCopy);
if (units[0].unit === 'E' && units.length === elfCount) {
console.log(result);
process.exit();
}
} while(true);