aboutsummaryrefslogtreecommitdiff
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
parenta5c7c011332b064814239191a7379b26fd15618a (diff)
downloadadvent-of-code-ec8fa76a38c46cf6e9b3c98bb28bbe15ed54140f.tar.gz
advent-of-code-ec8fa76a38c46cf6e9b3c98bb28bbe15ed54140f.zip
2015 day22
-rw-r--r--src/2015/day22/README.md5
-rw-r--r--src/2015/day22/aoc.cpp33
-rw-r--r--test/test_2015.cpp2
3 files changed, 16 insertions, 24 deletions
diff --git a/src/2015/day22/README.md b/src/2015/day22/README.md
index 6851645..e06224a 100644
--- a/src/2015/day22/README.md
+++ b/src/2015/day22/README.md
@@ -109,4 +109,9 @@ You start with 50 hit points and 500 mana points. The boss's actual stats are in
What is the least amount of mana you can spend and still win the fight?
(Do not include mana recharge effects as "spending" negative mana.)
+--- Part Two ---
+On the next run through the game, you increase the difficulty to hard.
+At the start of each player turn (before any other effects apply), you lose 1 hit point. If this brings you to or below 0 hit points, you lose.
+
+With the same starting stats for you and the boss, what is the least amount of mana you can spend and still win the fight?
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 {
diff --git a/test/test_2015.cpp b/test/test_2015.cpp
index eceef29..3a3f38f 100644
--- a/test/test_2015.cpp
+++ b/test/test_2015.cpp
@@ -231,5 +231,5 @@ TEST_CASE("Wizard Simulator 20XX", "[2015]") {
aoc2015::wizard boss;
boss.points = 55;
auto p = aoc2015::day22(me, boss);
- REQUIRE(0 == p.first);
+ REQUIRE(953 == p.first);
}