-
Notifications
You must be signed in to change notification settings - Fork 0
/
2015_22.cs
234 lines (193 loc) · 7.42 KB
/
2015_22.cs
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
namespace AOC.CSharp;
public static class AOC2015_22
{
private const int MagicMissileCost = 53;
private const int MagicMissileDamage = 4;
private const int DrainCost = 73;
private const int DrainDamage = 2;
private const int ShieldCost = 113;
private const int ShieldArmor = 7;
private const int ShieldTurns = 6;
private const int PoisonCost = 173;
private const int PoisonDamage = 3;
private const int PoisonTurns = 6;
private const int RechargeCost = 229;
private const int RechargeMana = 101;
private const int RechargeTurns = 5;
public static long Solve1(string[] lines, string extra)
{
int bossHp = int.Parse(lines[0].Split(':')[1]);
int bossDamage = int.Parse(lines[1].Split(':')[1]);
string[] splits = extra.Split(',');
int playerHp = int.Parse(splits[0]);
int playerMana = int.Parse(splits[1]);
State state = new State
{
BossHp = bossHp,
BossDamage = bossDamage,
PlayerHp = playerHp,
PlayerMana = playerMana,
};
int bestWinCost = int.MaxValue;
RecursePlayerTurn(state, ref bestWinCost, 0);
return bestWinCost;
}
public static long Solve2(string[] lines, string extra)
{
int bossHp = int.Parse(lines[0].Split(':')[1]);
int bossDamage = int.Parse(lines[1].Split(':')[1]);
string[] splits = extra.Split(',');
int playerHp = int.Parse(splits[0]);
int playerMana = int.Parse(splits[1]);
State state = new State
{
BossHp = bossHp,
BossDamage = bossDamage,
PlayerHp = playerHp,
PlayerMana = playerMana,
PlayerTurnSelfDamage = 1,
};
int bestWinCost = int.MaxValue;
RecursePlayerTurn(state, ref bestWinCost, 0);
return bestWinCost;
}
private static void RecursePlayerTurn(State initialState, ref int bestWinCost, int depth)
{
State afterSelfDamageState = initialState with
{
PlayerHp = initialState.PlayerHp - initialState.PlayerTurnSelfDamage
};
if (afterSelfDamageState.PlayerHp <= 0)
{
// LOSE: Ran out of health due to the self damage effect
return;
}
if (afterSelfDamageState.ManaSpent >= bestWinCost)
{
// SUBOPTIMAL: Already spent more mana than the current best solution
return;
}
State afterTimerState = HandleTimerActions(afterSelfDamageState);
if (afterTimerState.BossHp <= 0)
{
// WIN: New optimal solution. Boss was taken out by the poison timer
bestWinCost = afterTimerState.ManaSpent;
return;
}
// Recursively branch and explore each available spell path
if (afterTimerState.PlayerMana >= MagicMissileCost)
{
State spellState = afterTimerState with
{
BossHp = afterTimerState.BossHp - MagicMissileDamage,
ManaSpent = afterTimerState.ManaSpent + MagicMissileCost,
PlayerMana = afterTimerState.PlayerMana - MagicMissileCost,
};
RecurseBossTurn(spellState, ref bestWinCost, depth + 1);
}
if (afterTimerState.PlayerMana >= DrainCost)
{
State spellState = afterTimerState with
{
BossHp = afterTimerState.BossHp - DrainDamage,
PlayerHp = afterTimerState.PlayerHp + DrainDamage,
ManaSpent = afterTimerState.ManaSpent + DrainCost,
PlayerMana = afterTimerState.PlayerMana - DrainCost,
};
RecurseBossTurn(spellState, ref bestWinCost, depth + 1);
}
if (afterTimerState.PlayerMana >= ShieldCost && afterTimerState.ShieldTurns == 0)
{
State spellState = afterTimerState with
{
ManaSpent = afterTimerState.ManaSpent + ShieldCost,
PlayerMana = afterTimerState.PlayerMana - ShieldCost,
ShieldTurns = ShieldTurns,
};
RecurseBossTurn(spellState, ref bestWinCost, depth + 1);
}
if (afterTimerState.PlayerMana >= PoisonCost && afterTimerState.PoisonTurns == 0)
{
State spellState = afterTimerState with
{
ManaSpent = afterTimerState.ManaSpent + PoisonCost,
PlayerMana = afterTimerState.PlayerMana - PoisonCost,
PoisonTurns = PoisonTurns,
};
RecurseBossTurn(spellState, ref bestWinCost, depth + 1);
}
if (afterTimerState.PlayerMana >= RechargeCost && afterTimerState.RechargeTurns == 0)
{
State spellState = afterTimerState with
{
ManaSpent = afterTimerState.ManaSpent + RechargeCost,
PlayerMana = afterTimerState.PlayerMana - RechargeCost,
RechargeTurns = RechargeTurns,
};
RecurseBossTurn(spellState, ref bestWinCost, depth + 1);
}
}
private static void RecurseBossTurn(State initialState, ref int bestWinCost, int depth)
{
if (initialState.ManaSpent >= bestWinCost)
{
// SUBOPTIMAL: Already spent more mana than the current best solution
return;
}
if (initialState.BossHp <= 0)
{
// WIN: New optimal solution. Boss was taken out by the last player spell
bestWinCost = initialState.ManaSpent;
return;
}
State afterTimerState = HandleTimerActions(initialState);
if (afterTimerState.BossHp <= 0)
{
if (afterTimerState.ManaSpent < bestWinCost)
{
// WIN: New optimal solution. Boss was taken out by the poison timer
bestWinCost = afterTimerState.ManaSpent;
}
return;
}
int damageToPlayer = afterTimerState.BossDamage - afterTimerState.PlayerArmor;
State afterBossAttackState = afterTimerState with
{
PlayerHp = afterTimerState.PlayerHp - damageToPlayer,
};
if (afterBossAttackState.PlayerHp > 0)
{
// Continue playing as long as we are still alive
RecursePlayerTurn(afterBossAttackState, ref bestWinCost, depth + 1);
}
}
private static State HandleTimerActions(State state)
{
State newState = state;
int poisonDamage = state.PoisonTurns >= 1 ? PoisonDamage : 0;
int rechargeMana = state.RechargeTurns >= 1 ? RechargeMana : 0;
newState = newState with
{
PlayerArmor = state.ShieldTurns >= 1 ? ShieldArmor : 0,
ShieldTurns = Math.Max(state.ShieldTurns - 1, 0),
PoisonTurns = Math.Max(state.PoisonTurns - 1, 0),
RechargeTurns = Math.Max(state.RechargeTurns - 1, 0),
PlayerMana = state.PlayerMana + rechargeMana,
BossHp = state.BossHp - poisonDamage
};
return newState;
}
}
record State
{
public int BossHp { get; init; }
public int BossDamage { get; init; }
public int PlayerHp { get; init; }
public int PlayerMana { get; init; }
public int PlayerArmor { get; init; }
public int ManaSpent { get; init; }
public int PoisonTurns { get; init; }
public int ShieldTurns { get; init; }
public int RechargeTurns { get; init; }
public int PlayerTurnSelfDamage { get; init; }
}