-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge07.cpp
78 lines (69 loc) · 2.31 KB
/
challenge07.cpp
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
// AquaQ Challenge Hub
// Challenge 7: What is best in life?
// https://challenges.aquaq.co.uk/challenge/7
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <gsl/util>
static bool readFile(const std::string& fileName, std::vector<std::string>& lines)
{
std::ifstream in{fileName};
if (!in) {
std::cerr << "Cannot open file " << fileName << std::endl;
return false;
}
auto closeStream = gsl::finally([&in] { in.close(); });
std::string str;
while (std::getline(in, str)) {
lines.push_back(str);
}
return true;
}
using Rating = double;
using Map = std::map<std::string, Rating>;
void updateRatings(Map& map, const std::string& winner, const std::string& loser)
{
Rating rWinner{1200};
if (auto it = map.find(winner); it != map.end()) {
rWinner = it->second;
}
Rating rLoser{1200};
if (auto it = map.find(loser); it != map.end()) {
rLoser = it->second;
}
const double update{20 * (1 - (1. / (1 + std::pow(10, (rLoser - rWinner) / 400))))};
map[winner] = rWinner + update;
map[loser] = rLoser - update;
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc == 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
Map map{};
for (size_t i = 1; i < lines.size(); ++i) {
const auto& line = lines[i];
const auto pos1 = line.find(',', 0);
const auto player1 = line.substr(0, pos1);
const auto pos2 = line.find(',', pos1 + 1);
const auto player2 = line.substr(pos1 + 1, pos2 - pos1 - 1);
const auto pos3 = line.find('-', pos2 + 1);
const auto points1 = std::stoi(line.substr(pos2 + 1, pos3 - pos2 - 1));
const auto points2 = std::stoi(line.substr(pos3 + 1, line.size() - pos3 - 1));
if (points1 < points2) {
updateRatings(map, player2, player1);
} else if (points1 > points2) {
updateRatings(map, player1, player2);
}
}
auto [min, max] =
std::minmax_element(map.cbegin(), map.cend(), [](const auto& a, const auto& b) { return a.second < b.second; });
std::cout << static_cast<uint32_t>(max->second) - static_cast<uint32_t>(min->second) << std::endl;
return EXIT_SUCCESS;
}