diff options
author | kaiwu <kaiwu2004@gmail.com> | 2022-04-02 01:56:58 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2022-04-02 01:56:58 +0800 |
commit | aa64dbe8a6c980da4476ea8549a0508d29b0027d (patch) | |
tree | 907989d9490f24efcaaed20f79d8e969e77e9591 /src | |
parent | 4348c6981803d7004d5246370e95d2dca97d790c (diff) | |
download | advent-of-code-aa64dbe8a6c980da4476ea8549a0508d29b0027d.tar.gz advent-of-code-aa64dbe8a6c980da4476ea8549a0508d29b0027d.zip |
apply spell
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day22/aoc.h | 74 |
1 files changed, 44 insertions, 30 deletions
diff --git a/src/2015/day22/aoc.h b/src/2015/day22/aoc.h index d2ccd7d..44b7e93 100644 --- a/src/2015/day22/aoc.h +++ b/src/2015/day22/aoc.h @@ -25,31 +25,36 @@ struct wizard { }; struct spell { + wizard* w; + spell(wizard* wp) : w(wp) {} virtual int cost() = 0; virtual void apply(wizard&) = 0; virtual void unapply(wizard&) = 0; }; struct magic_missile : public spell { + magic_missile(wizard* w) : spell(w) {} virtual int cost() override { return 53; }; virtual void apply(wizard& w) override { w.hitpoints -= 4; } virtual void unapply(wizard& w) override { w.hitpoints += 4; } }; -struct drain_heal : public spell { +struct drain : public spell { + drain(wizard* w) : spell(w) {} virtual int cost() override { return 73; }; - virtual void apply(wizard& w) override { w.hitpoints += 2; } - virtual void unapply(wizard& w) override { w.hitpoints -= 2; } -}; - -struct drain_hurt : public spell { - virtual int cost() override { return 0; }; - virtual void apply(wizard& w) override { w.hitpoints -= 2; } - virtual void unapply(wizard& w) override { w.hitpoints += 2; } + virtual void apply(wizard& w) override { + w.hitpoints -= 2; + this->w->hitpoints += 2; + } + virtual void unapply(wizard& w) override { + w.hitpoints += 2; + this->w->hitpoints -= 2; + } }; struct shield : public spell { int armor; + shield(wizard* w) : spell(w) {} virtual int cost() override { return 113; }; virtual void apply(wizard& w) override { armor = w.armor; @@ -59,52 +64,61 @@ struct shield : public spell { }; struct poison : public spell { + poison(wizard* w) : spell(w) {} virtual int cost() override { return 173; }; virtual void apply(wizard& w) override { w.hitpoints -= 3; } virtual void unapply(wizard& w) override { w.hitpoints += 3; } }; struct recharge : public spell { + recharge(wizard* w) : spell(w) {} virtual int cost() override { return 229; }; virtual void apply(wizard& w) override { w.mana += 101; } virtual void unapply(wizard& w) override { w.mana -= 101; } }; struct arena { - void cast(int s, wizard& w1, wizard& w2) { + void apply(wizard* w) { + for (int i = 0; i < 5; i++) { + if (!w->spells[i].empty()) { + spell* s = w->spells[i].top(); + s->apply(*w); + w->spells[i].pop(); + } + } + } + + // before cast needs to check + // 1 spell s is not active, stack is empty + // 2 w2 has enough mana + void cast(int s, wizard* w1, wizard* w2) { switch (stype(s)) { case stype::magic_missile: - if (w1.spells[s].empty()) { - w1.spells[s].emplace(new magic_missile); - } + w1->spells[s].emplace(new magic_missile{w2}); + w2->mana -= w1->spells[s].top()->cost(); break; case stype::drain: - if (w1.spells[s].empty() && w2.spells[s].empty()) { - w1.spells[s].emplace(new drain_hurt); - w2.spells[s].emplace(new drain_heal); - } + w1->spells[s].emplace(new drain{w2}); + w2->mana -= w1->spells[s].top()->cost(); break; case stype::shield: - if (w1.spells[s].empty()) { - for (int i = 0; i < 6; i++) { - w1.spells[s].emplace(new shield); - } + for (int i = 0; i < 6; i++) { + w2->spells[s].emplace(new shield{w2}); } + w2->mana -= w2->spells[s].top()->cost(); break; case stype::poison: - if (w1.spells[s].empty()) { - for (int i = 0; i < 6; i++) { - w1.spells[s].emplace(new poison); - } + for (int i = 0; i < 6; i++) { + w1->spells[s].emplace(new poison{w2}); } + w2->mana -= w1->spells[s].top()->cost(); break; case stype::recharge: - if (w1.spells[s].empty()) { - for (int i = 0; i < 5; i++) { - w1.spells[s].emplace(new recharge); - } - break; + for (int i = 0; i < 5; i++) { + w1->spells[s].emplace(new recharge{w2}); } + w2->mana -= w1->spells[s].top()->cost(); + break; } } }; |