-
Notifications
You must be signed in to change notification settings - Fork 17
/
part-two.js
114 lines (90 loc) · 3.28 KB
/
part-two.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
let initial_state = {
boss: 51,
damage: 9,
hp: 50,
mana: 500,
armor: 0,
manaSpent: 0,
shield: 0,
poison: 0,
recharge: 0,
move: 'player',
};
const MISSILE = 'missile';
const DRAIN = 'drain';
const SHIELD = 'shield';
const POISON = 'poison';
const RECHARGE = 'recharge';
const spells = {
[MISSILE]: { cost: 53, damage: 4, heal: 0 },
[DRAIN]: { cost: 73, damage: 2, heal: 2 },
[SHIELD]: { cost: 113, duration: 6 },
[POISON]: { cost: 173, duration: 6 },
[RECHARGE]: { cost: 229, duration: 5 },
};
let min = Number.MAX_SAFE_INTEGER;
let moves = [initial_state];
let state;
while ((state = moves.pop())) {
// On hard mode, player loses 1 HP each turn
if (state.move === 'player') {
state.hp--;
}
// Check effects and apply them if they are greater than 0
// This ends up reducing these effects below zero in some cases but it doesn't end up affecting anything
state.armor = state.shield-- > 0 ? 7 : 0;
if (state.poison-- > 0) {
state.boss -= 3;
}
if (state.recharge-- > 0) {
state.mana += 101;
}
// If we've lost all our HP, or we've spent more mana than our current minimum we've found, exit this path
if (state.hp <= 0 || state.manaSpent >= min) {
continue;
}
// If our boss is dead, see if our mana spent is less than our current minimum
if (state.boss <= 0) {
min = Math.min(min, state.manaSpent);
continue;
}
if (state.move === 'boss') {
// Switch to player for next move
state.move = 'player';
// Boss always does at least 1 damage
state.hp -= Math.max(1, state.damage - state.armor);
moves.push(state);
// console.log(' Boss { ' + Object.keys(state).map(k => `${k === 'move' ? k + ': "' + state[k] + '"' : k + ': ' + state[k]}`).join(', ') + ' }');
} else {
// Switch to boss for next move
state.move = 'boss';
// We are on the player, loop through all the spells and push them to our possible moves
for (let spell in spells) {
let { cost, damage, heal, duration } = spells[spell];
// If this costs more than the mana we have, skip it
// Note that if we spend all out mana, this effectively kills the branch because
// all spells will be skipped and we'll never `push` a new state onto our moves array
if (cost >= state.mana) {
continue;
}
// Clone our current state
let new_state = Object.assign({}, state);
new_state.mana -= cost;
new_state.manaSpent += cost;
if (spell === MISSILE || spell === DRAIN) {
// Missle and Drain have no effect, they are instant spells
new_state.boss -= damage;
new_state.hp += heal;
} else {
// If we've already activated this effect, skip it
if (new_state[spell] > 0) {
continue;
}
new_state[spell] = duration;
}
moves.push(new_state);
// console.log('Player { ' + Object.keys(state).map(k => `${k === 'move' ? k + ': "' + n[k] + '"' : k + ': ' + n[k]}`).join(', ') + ' }');
}
}
}
console.log(min);