-
Notifications
You must be signed in to change notification settings - Fork 0
/
day22.js
152 lines (149 loc) · 3.68 KB
/
day22.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
// problem: http://adventofcode.com/day/22
// input: http://adventofcode.com/day/22/input
var boss = {
initialHp: 55,
reset: function() {
this.hp=this.initialHp;
this.damage=8;
this.poisonTimer=0;
return this;
},
poisonEffect: function() {
if (this.poisonTimer > 0) {
this.hp -= 3;
this.poisonTimer--;
}
return this.hp;
},
toString: function() {
return 'hp:'+this.hp+' pt:'+this.poisonTimer;
}
};
var hero = {
initialHp: 50,
initialMana: 500,
reset: function() {
this.hp=this.initialHp;
this.armor=0;
this.mana=this.initialMana;
this.rechargeTimer = 0;
this.shieldTimer = 0;
return this;
},
toString: function() {
return 'hp:'+this.hp+' ar:'+this.armor+' m:'+this.mana+' rt:'+this.rechargeTimer+' st:'+this.shieldTimer;
},
getHit: function(attacker) {
this.hp -= Math.max(1, attacker.damage - this.armor);
return this.hp;
},
shieldEffect: function() {
if (this.shieldTimer === 1) {
this.armor = 0;
}
if (this.shieldTimer > 0) {
this.shieldTimer--;
}
},
rechargeEffect: function() {
if (this.rechargeTimer > 0) {
this.mana += 101;
this.rechargeTimer--;
}
}
};
function magicMissile(hero, boss) {
if (hero.mana < 53) {
return Infinity;
}
boss.hp -= 4;
return 53;
}
function drain(hero, boss) {
if (hero.mana < 73) {
return Infinity;
}
hero.hp += 2;
boss.hp -= 2;
return 73;
}
function shield(hero, boss) {
if (hero.mana < 113 || hero.shieldTimer > 0) {
return Infinity;
}
hero.shieldTimer = 6;
hero.armor = 7;
return 113;
}
function poison(hero, boss) {
if (hero.mana < 173 || boss.poisonTimer > 0) {
return Infinity;
}
boss.poisonTimer = 6;
return 173;
}
function recharge(hero, boss) {
if (hero.mana < 229 || hero.rechargeTimer > 0) {
return Infinity;
}
hero.rechargeTimer = 5;
return 229;
}
var actions = [ magicMissile, drain, shield, poison, recharge ];
// return mana spent if a winner (under limit), otherwise Infinity
function heroSpentMana(hero, boss, actionPlan, limit, hard) {
if (limit === undefined) {
limit = Infinity;
}
var manaSpent = 0;
hero.reset();
boss.reset();
while (true) {
// player turn
if (hard && --hero.hp <= 0) {
return Infinity;
}
if (boss.poisonEffect() <= 0) {
return manaSpent;
}
hero.rechargeEffect();
hero.shieldEffect();
var a = actionPlan % actions.length;
// console.log(['magicMissile','drain','shield','poison','recharge'][a]);
actionPlan = Math.floor(actionPlan/actions.length);
var spent = actions[a](hero, boss); // Infinity when impossible (no mana or effect already active)
manaSpent += spent;
if (manaSpent >= limit) {
// console.log(hero);
return Infinity;
}
hero.mana -= spent;
// console.log('Hero:'+hero.toString()+'\t\tBoss:'+boss.toString());
// boss turn
if (boss.poisonEffect() <= 0) {
return manaSpent;
}
hero.rechargeEffect();
hero.shieldEffect();
if (hero.getHit(boss) <= 0) {
//console.log(hero);
return Infinity;
}
// console.log('Hero:'+hero.toString()+'\t\tBoss:'+boss.toString());
}
}
//Action plan 448 (3423) succeeded and cost 953
function findMinManaSpent(hard) {
var minManaSpent = Infinity;
var bestPlan = Infinity;
for (var p = 1; p <= 188926; p++) {
var spent = heroSpentMana(hero, boss, p, minManaSpent, hard);
if (spent < minManaSpent) {
minManaSpent = spent;
bestPlan = p;
var planSteps = bestPlan.toString(5).split("").reverse().join("");
console.log('Action plan ' + bestPlan + ' (' + planSteps + ') succeeded and cost ' + minManaSpent);
}
}
return minManaSpent;
}