aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day22.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2015/src/day22.cpp')
-rw-r--r--2015/src/day22.cpp84
1 files changed, 80 insertions, 4 deletions
diff --git a/2015/src/day22.cpp b/2015/src/day22.cpp
index 85b7259..b7a23d0 100644
--- a/2015/src/day22.cpp
+++ b/2015/src/day22.cpp
@@ -17,8 +17,8 @@ auto& debug = cerr;
17struct NullBuffer : streambuf { 17struct NullBuffer : streambuf {
18 int overflow(int chr) override { return chr; } 18 int overflow(int chr) override { return chr; }
19}; 19};
20NullBuffer nullBuffer; 20NullBuffer nullBuffer; // NOLINT
21ostream debug{ &nullBuffer }; 21ostream debug{ &nullBuffer }; // NOLINT
22#endif 22#endif
23 23
24using Mana = int; 24using Mana = int;
@@ -121,6 +121,7 @@ public:
121 } 121 }
122 122
123 Mana mana_; 123 Mana mana_;
124 Mana mana_bought_{};
124 HitPoints hit_points_; 125 HitPoints hit_points_;
125 Armor armor_{}; 126 Armor armor_{};
126}; 127};
@@ -133,6 +134,7 @@ public:
133 { 134 {
134 debug << "Player casts " << spell->name_ << endl; 135 debug << "Player casts " << spell->name_ << endl;
135 player.SubMana(spell->mana_); 136 player.SubMana(spell->mana_);
137 player.mana_bought_ += spell->mana_;
136 138
137 if ( spell->Timer() == 0 ) { 139 if ( spell->Timer() == 0 ) {
138 spell->Play(player, boos); 140 spell->Play(player, boos);
@@ -142,7 +144,7 @@ public:
142 } 144 }
143 } 145 }
144 146
145 bool TryAddRandomSpell(Player& player, Boss& boss) 147 [[nodiscard]] bool TryAddRandomSpell(Player& player, Boss& boss)
146 { 148 {
147 while ( true ) { 149 while ( true ) {
148 auto randomSpell = chooseRandomSpell(player); 150 auto randomSpell = chooseRandomSpell(player);
@@ -156,6 +158,8 @@ public:
156 } 158 }
157 159
158 AddSpell(randomSpell, player, boss); 160 AddSpell(randomSpell, player, boss);
161
162 return true;
159 } 163 }
160 } 164 }
161 165
@@ -553,8 +557,80 @@ battle2()
553 cout << "End of Battle" << endl; 557 cout << "End of Battle" << endl;
554} 558}
555 559
560enum RoundState {
561 Player_Lost,
562 Boss_Lost,
563 Undecided
564};
565
566RoundState
567battle_round(Player& player, Boss& boss, Spells& spells)
568{
569 debug << "\n-- Player turn --\n"
570 << player << '\n'
571 << boss << endl;
572
573 spells.Play(player, boss);
574 if ( player.HasLost() ) {
575 return Player_Lost;
576 }
577 if ( boss.HasLost() ) {
578 return Boss_Lost;
579 }
580 if ( !spells.TryAddRandomSpell(player, boss) ) {
581 return Player_Lost;
582 }
583
584 // ----------------------
585
586 debug << "\n-- Boss turn --\n"
587 << player << '\n'
588 << boss << endl;
589
590 spells.Play(player, boss);
591 if ( player.HasLost() ) {
592 return Player_Lost;
593 }
594 if ( boss.HasLost() ) {
595 return Boss_Lost;
596 }
597 boss.Play(player);
598 if ( player.HasLost() ) {
599 return Player_Lost;
600 }
601 return Undecided;
602}
603
604void
605part1()
606{
607 const Boss boss_master("data/day22.txt");
608
609 static const Mana start_mana = 500;
610 static const HitPoints start_hitpoints = 50;
611
612 auto min_mana{ 999999 };
613
614 for ( int i = 0; i != 500000; ++i ) {
615 Boss boss{ boss_master };
616 Player player(start_mana, start_hitpoints);
617 Spells spells;
618
619 auto roundState = battle_round(player, boss, spells);
620 while ( roundState == Undecided ) {
621 roundState = battle_round(player, boss, spells);
622 }
623
624 if ( roundState == Boss_Lost ) {
625 min_mana = min(min_mana, player.mana_bought_);
626 }
627 }
628 cout << min_mana << endl;
629}
630
556int 631int
557main() 632main()
558{ 633{
559 battle2(); 634 srand((unsigned int) time(NULL));
635 part1();
560} 636}