1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 14:16:55 +02:00
CodeWars/5kyu/greed_is_good/solution.cpp
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

48 lines
875 B
C++

#include <map>
#include <tuple>
#include <vector>
std::map<int, int> process_dice(const std::vector<int>& dice) {
std::map<int, int> freqs;
for (auto val : dice) {
freqs[val]++;
}
return freqs;
}
int score(const std::vector<int>& dice) {
const std::map<std::tuple<int, int>, int> options{
{{3, 1}, 1000},
{{3, 6}, 600},
{{3, 5}, 500},
{{3, 4}, 400},
{{3, 3}, 300},
{{3, 2}, 200}
};
auto counts = process_dice(dice);
auto total = 0;
for (auto has_changed = true; has_changed;) {
has_changed = false;
for (const auto& [option, points] : options) {
const auto& [count, die] = option;
if (counts[die] >= count) {
counts[die] -= count;
total += points;
has_changed = true;
break;
}
}
}
total += counts[1] * 100;
total += counts[5] * 50;
return total;
}