aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkaiwu <kaiwu2004@gmail.com>2022-12-08 19:22:13 +0800
committerkaiwu <kaiwu2004@gmail.com>2022-12-08 19:22:13 +0800
commitead6f2df98f27d509b1365c6d34b52abb063e303 (patch)
tree9f3dcb029e1d8172d995505e254fc9620a48dea1 /src
parentb2004336e7a9971b969de0302cf58af2d08c052a (diff)
downloadadvent-of-code-ead6f2df98f27d509b1365c6d34b52abb063e303.tar.gz
advent-of-code-ead6f2df98f27d509b1365c6d34b52abb063e303.zip
2015 day22 part fix
Diffstat (limited to 'src')
-rw-r--r--src/2015/day22/aoc.cpp59
1 files changed, 42 insertions, 17 deletions
diff --git a/src/2015/day22/aoc.cpp b/src/2015/day22/aoc.cpp
index 683d6bd..e7d101c 100644
--- a/src/2015/day22/aoc.cpp
+++ b/src/2015/day22/aoc.cpp
@@ -25,24 +25,21 @@ std::vector<spell*> can_spell(const wizard& w) {
return is;
}
-// effect for wizard w
-void effects(wizard& w) {
- for (int i = 0; i < 5; i++) {
- spell* s = w.spells[i];
- if (s != nullptr && s->tick > 0) {
- w.points -= s->damage < w.armor ? 1 : s->damage;
- w.wp->points += s->heals;
- w.wp->mana += s->payback;
- w.wp->armor += s->protect;
- s->tick -= 1;
- }
+char * indent(int t) {
+ static char idn[60] = {0};
+ memset(idn, 0, 60);
+ for (int i = 0; i < ((t+1) / 2) * 2; i++) {
+ idn[i] = ' ';
}
+ return idn;
}
-void start_of_each_turn (wizard& me, wizard& boss) {
+void start_of_each_turn (int t, wizard& me, wizard& boss) {
+ printf("%sT[%d] boss[%d] player[%d] armor[%d] mana[%d]\n", indent(t), t, boss.points, me.points, me.armor, me.mana);
for (int i = 2; i < 5; i++) {
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, "Shield") == 0) {
me.armor = 7;
}
@@ -74,26 +71,54 @@ void instant_effect(spell* s, wizard& me, wizard& boss) {
}
}
+void restore_effect(spell* s, wizard& me, wizard& boss) {
+ if (strcmp(s->name, "Magic Missile") == 0) {
+ boss.points += s->damage;
+ s->tick += 1;
+ }
+ if (strcmp(s->name, "Drain") == 0) {
+ boss.points += s->damage;
+ me.points -= s->heals;
+ s->tick += 1;
+ }
+}
+
+void save_ticks(int ticks[], spell* spells[]) {
+ for(int i = 0; i < 5; i++) {
+ ticks[i] = spells[i]->tick;
+ }
+}
+
+void undo_ticks(int ticks[], spell* spells[]) {
+ for(int i = 0; i < 5; i++) {
+ spells[i]->tick = ticks[i];
+ }
+}
+
void fight(int turn, wizard me, wizard boss, int cost, std::vector<int>& costs) {
- start_of_each_turn(me, boss);
+ start_of_each_turn(turn, me, boss);
if (turn % 2 == 1) { // my turn
- // printf("player points[%d] armor[%d] mana[%d]\n", me.points, me.armor, me.mana);
if (me.points > 0) {
auto ss = can_spell(me);
for(auto& s : ss) {
- printf("choose %s\n", s->name);
+ 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
- printf("boss points[%d]\n", boss.points);
if (boss.points <= 0) { // boss lose
- printf("player win %d\n", cost);
+ printf("%splayer win %d\n", indent(turn), cost);
costs.push_back(cost);
}
else {