-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21-RPG-Simulator-20XX.cpp
More file actions
135 lines (111 loc) · 4.26 KB
/
21-RPG-Simulator-20XX.cpp
File metadata and controls
135 lines (111 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2023 Joe Baker (JoeBlakeB)
// Advent of Code 2015 - Day 21: RPG Simulator 20XX
// Usage:
// scripts/cppRun.sh 2015/21-RPG-Simulator-20XX.cpp < 2015/inputs/21.txt
#include <array>
#include <climits>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "utils.cpp"
using namespace std;
/*
* For the input, have the three paragraphs for the shops selling
* and a paragraph for boss's stats in the input with one empty line between.
*/
struct Item {
string name;
const int cost, damage, armor;
};
class Character {
public:
int hitPoints, damage, armor;
Character(int hitPoints, int damage, int armor)
: hitPoints(hitPoints), damage(damage), armor(armor) {}
Character(vector<string> input) : Character(
stoi(input[0].substr(12)),
stoi(input[1].substr(8)),
stoi(input[2].substr(7))
) {}
bool operator>(const Character& enemyOriginal) const {
Character me = *this;
Character enemy = enemyOriginal;
unsigned int currentTurn = 0;
for (; me.hitPoints > 0 && enemy.hitPoints > 0; currentTurn = (currentTurn + 1) % 2) {
Character& attacker = currentTurn == 0 ? me : enemy;
Character& defender = currentTurn == 0 ? enemy : me;
int attackDamage = attacker.damage - defender.armor;
if (attackDamage < 1) attackDamage = 1;
defender.hitPoints -= attackDamage;
}
return me.hitPoints > 0;
}
};
vector<Item> getItemSet(vector<string> input) {
vector<Item> items;
for (size_t i = 1; i < input.size(); i++) {
istringstream iss(input[i]);
string name;
int values [3];
for (int j = 0; j < 3;) {
string word;
iss >> word;
if (isdigit(word[0])) {
values[j++] = stoi(word);
} else {
name += word;
}
}
items.push_back(
Item({name, values[0], values[1], values[2]})
);
}
return items;
}
int main() {
vector<Item> weapons = getItemSet(getInputLinesVector());
vector<Item> armorSets = getItemSet(getInputLinesVector());
vector<Item> rings = getItemSet(getInputLinesVector());
Character boss(getInputLinesVector());
armorSets.push_back(Item({"None", 0, 0, 0}));
rings.push_back(Item({"None", 0, 0, 0}));
int lowestCost = INT_MAX;
int highestCost = INT_MIN;
array<string, 4> lowestCostItems;
array<string, 4> highestCostItems;
for (Item& weapon : weapons) {
for (Item& armor : armorSets) {
for (int r1 = rings.size()-1; r1 >= 0; r1--) {
int r2 = rings.size()-1;
do {
int cost = weapon.cost + armor.cost + rings[r1].cost + rings[r2].cost;
if (cost < lowestCost || cost > highestCost) {
Character player(100,
weapon.damage + rings[r1].damage + rings[r2].damage,
armor.armor + rings[r1].armor + rings[r2].armor);
if (player > boss && cost < lowestCost) {
lowestCost = cost;
lowestCostItems = {weapon.name, armor.name,
rings[r1].name, rings[r2].name};
}
if (!(player > boss) && cost > highestCost) {
highestCost = cost;
highestCostItems = {weapon.name, armor.name,
rings[r1].name, rings[r2].name};
}
}
} while (--r2 > r1);
}
}
}
cout << "The least amounts of gold you can spend to win is: "
<< lowestCost << endl;
cout << "With: " << lowestCostItems[0] << ", " << lowestCostItems[1] << ", "
<< lowestCostItems[2] << ", " << lowestCostItems[3] << "\n" << endl;
cout << "The most amounts of gold you can spend but still lose is: "
<< highestCost << endl;
cout << "With: " << highestCostItems[0] << ", " << highestCostItems[1] << ", "
<< highestCostItems[2] << ", " << highestCostItems[3] << endl;
return 0;
}