-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
213 lines (192 loc) · 4.27 KB
/
main.cpp
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
/************************************************************************/
/* Advent of Code:
/* Day 22: Wizard Simulator 20XX
/************************************************************************/
#include <algorithm>
#include <iostream>
#include <chrono>
#include <string>
#include <array>
class CGame
{
public:
enum class RESULT {
PLOST = 1,
PWON = 2,
SCAST = 4,
};
enum class SPELL {
RECHARGE = 0,
POISON = 1,
SHIELD = 2,
MMISSLE = 3,
DRAIN = 4,
};
typedef struct _Spell
{
SPELL name;
int mana_cost;
int damage;
int armour;
int health;
int mana;
int duration;
} Spell;
typedef struct _State
{
int hp;
int mana;
int spentMana;
int bossHP;
std::array<Spell, 3> effects;
} State;
public:
CGame() {};
CGame(int hp, int damage, int hard=0) :
m_bossDamage(damage)
, m_hard(hard)
, m_armour(0)
, m_lowestMana(5000)
{
m_gameState.hp = 50;
m_gameState.bossHP = hp;
m_gameState.mana = 500;
m_gameState.spentMana = 0;
m_gameState.effects = { {
{ SPELL::RECHARGE, 229, 0, 0, 0, 101, 0 },
{ SPELL::POISON, 173, 3, 0, 0, 0, 0 },
{ SPELL::SHIELD, 113, 0, 7, 0, 0, 0 }
} };
};
~CGame() {};
int startGame();
int getSpentMana() { return m_lowestMana; };
private:
RESULT effects();
RESULT castSpell(const Spell& spell);
RESULT turn(const Spell& spell);
bool success(RESULT res) { return (res != RESULT::PLOST); };
bool won(RESULT res) { return (res == RESULT::PWON); };
bool lost(RESULT res) { return (res == RESULT::PLOST); };
private:
std::array<Spell, 5> m_spells = { {
{SPELL::MMISSLE, 53, 4, 0, 0, 0, 0},
{SPELL::DRAIN, 73, 2, 0, 2, 0, 0},
{SPELL::SHIELD, 113, 0, 7, 0, 0, 6},
{SPELL::POISON, 173, 3, 0, 0, 0, 6},
{SPELL::RECHARGE, 229, 0, 0, 0, 101, 5}
} };
State m_gameState;
int m_bossDamage;
int m_armour;
int m_hard;
int m_lowestMana;
};
CGame::RESULT CGame::effects()
{
m_armour = 0;
for (auto& ev : m_gameState.effects)
{
if (ev.duration)
{
ev.duration--;
m_armour = ev.armour;
m_gameState.bossHP -= ev.damage;
m_gameState.mana += ev.mana;
}
if (m_gameState.bossHP <= 0)
return RESULT::PWON;
}
return RESULT::SCAST;
}
CGame::RESULT CGame::castSpell(const Spell& spell)
{
if (spell.mana_cost <= m_gameState.mana)
{
m_gameState.mana -= spell.mana_cost;
m_gameState.spentMana += spell.mana_cost;
if (spell.duration)
{
if (spell.name != SPELL::MMISSLE && spell.name != SPELL::DRAIN && m_gameState.effects[std::underlying_type<RESULT>::type(spell.name)].duration == 0)
{
m_gameState.effects[std::underlying_type<RESULT>::type(spell.name)] = spell;
}
}
else
{
m_gameState.bossHP -= spell.damage;
m_gameState.hp += spell.health;
}
}
else
{
return RESULT::PLOST;
}
if (m_gameState.bossHP <= 0)
{
return RESULT::PWON;
}
return RESULT::SCAST;
}
CGame::RESULT CGame::turn(const Spell& spell)
{
RESULT lastRes = RESULT::SCAST;
m_gameState.hp -= m_hard;
if (m_gameState.hp <= 0)
return RESULT::PLOST;
if (won(effects()) || won(lastRes = castSpell(spell)) || (!lost(lastRes) && won(effects())))
{
m_lowestMana = std::min(m_lowestMana, m_gameState.spentMana);
return RESULT::PWON;
}
if (lost(lastRes))
{
return lastRes;
}
m_gameState.hp = m_gameState.hp - std::max((m_bossDamage - m_armour), 1);
if (m_gameState.hp <= 0 || m_gameState.mana <= 0 || lost(lastRes))
{
return RESULT::PLOST;
}
for (const auto& sp : m_spells)
{
if (!sp.duration || sp.name != spell.name)
{
State state = m_gameState;
lastRes = turn(sp);
m_gameState = state;
}
}
return lastRes;
}
int CGame::startGame()
{
for (auto& spell : m_spells)
{
State state = m_gameState;
turn(spell);
m_gameState = state;
}
return 0;
}
int partOne()
{
CGame game(51, 9);
game.startGame();
return game.getSpentMana();
}
int partTwo()
{
CGame game(51, 9, 1);
game.startGame();
return game.getSpentMana();
}
int main()
{
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
std::cout << "Part One: " << partOne() << std::endl;
std::cout << "Part Two: " << partTwo() << std::endl;
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - t1);
std::cout << "Time: " << time_span.count() << "s.";
return 0;
}