aboutsummaryrefslogtreecommitdiff
path: root/src/2015/day22/aoc.cpp
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-08 20:26:50 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-08 20:26:50 +0800
commitec8fa76a38c46cf6e9b3c98bb28bbe15ed54140f (patch)
tree6c2dd84e97229ad78e47608e5d2753a0e90046d8 /src/2015/day22/aoc.cpp
parenta5c7c011332b064814239191a7379b26fd15618a (diff)
downloadadvent-of-code-ec8fa76a38c46cf6e9b3c98bb28bbe15ed54140f.tar.gz
advent-of-code-ec8fa76a38c46cf6e9b3c98bb28bbe15ed54140f.zip
2015 day22
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 {