aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day22/aoc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/2015/day22/aoc.cpp')
-rw-r--r--src/2015/day22/aoc.cpp33
1 files changed, 10 insertions, 23 deletions
diff --git a/src/2015/day22/aoc.cpp b/src/2015/day22/aoc.cpp
index c89ceac..55274e5 100644
--- a/src/2015/day22/aoc.cpp
+++ b/src/2015/day22/aoc.cpp
@@ -40,6 +40,13 @@ void start_of_each_turn (int t, wizard& me, wizard& boss) {
spell* s = boss.spells[i];
if (s->tick > 0) {
// printf("%sT[%d] %s has tick %d\n", indent(t), t, s->name, s->tick);
+ if (strcmp(s->name, "Magic Missile") == 0 && t % 2 == 0) {
+ boss.points -= s->damage;
+ }
+ if (strcmp(s->name, "Drain") == 0 && t % 2 == 0) {
+ boss.points -= s->damage;
+ me.points += s->heals;
+ }
if (strcmp(s->name, "Shield") == 0) {
me.armor = 7;
}
@@ -59,26 +66,6 @@ void start_of_each_turn (int t, wizard& me, wizard& boss) {
}
}
-void instant_effect(spell* s, wizard& me, wizard& boss) {
- if (strcmp(s->name, "Magic Missile") == 0) {
- boss.points -= s->damage;
- }
- if (strcmp(s->name, "Drain") == 0) {
- boss.points -= s->damage;
- me.points += s->heals;
- }
-}
-
-void restore_effect(spell* s, wizard& me, wizard& boss) {
- if (strcmp(s->name, "Magic Missile") == 0) {
- boss.points += s->damage;
- }
- if (strcmp(s->name, "Drain") == 0) {
- boss.points += s->damage;
- me.points -= s->heals;
- }
-}
-
void save_ticks(int ticks[], spell* spells[]) {
for(int i = 0; i < 5; i++) {
ticks[i] = spells[i]->tick;
@@ -93,28 +80,28 @@ void undo_ticks(int ticks[], spell* spells[]) {
void fight(int turn, wizard me, wizard boss, int cost, std::vector<int>& costs) {
start_of_each_turn(turn, me, boss);
+ if (turn > 18) return; // !!! THIS IS IMPORTANT !!!
if (turn % 2 == 1) { // my turn
+ // me.points -= 1; // part 2
if (me.points > 0) {
auto ss = can_spell(me);
for(auto& s : ss) {
// printf("%sT[%d] choose %s =============\n", indent(turn), turn, s->name);
s->tick = s->turns;
me.mana -= s->costs;
- instant_effect(s, me, boss);
int ticks[5] = {0};
save_ticks(ticks, boss.spells);
fight(turn+1, me, boss, cost + s->costs, costs);
undo_ticks(ticks, boss.spells);
- restore_effect(s, me, boss);
me.mana += s->costs;
s->tick = 0;
}
}
} else { // boss turn
if (boss.points <= 0) { // boss lose
- // printf("%splayer win %d\n", indent(turn), cost);
+ // printf("%sT[%d] player win %d\n", indent(turn), turn, cost);
costs.push_back(cost);
}
else {