|
| 1 | +#include "yacht.h" |
| 2 | + |
| 3 | +#include <algorithm> |
| 4 | +#include <numeric> |
| 5 | +#include <stdexcept> |
| 6 | + |
| 7 | +namespace yacht { |
| 8 | + |
| 9 | +int simple_scores(const std::array<int, 5>& dice, int target) { |
| 10 | + return std::accumulate( |
| 11 | + dice.begin(), dice.end(), 0, |
| 12 | + [target](int acc, int i) { return i == target ? acc + i : acc; }); |
| 13 | +} |
| 14 | + |
| 15 | +bool is_yacht(const std::array<int, 5>& dice) { |
| 16 | + return std::all_of(dice.begin(), dice.end(), |
| 17 | + [&dice](int i) { return i == dice[0]; }); |
| 18 | +} |
| 19 | + |
| 20 | +bool is_full_house(const std::array<int, 5>& dice) { |
| 21 | + const int min{dice.at(0)}; |
| 22 | + const int max{dice.at(4)}; |
| 23 | + if (min == max) return false; |
| 24 | + std::array<int, 5> full_house{min, min, max, max, max}; |
| 25 | + if (dice == full_house) return true; |
| 26 | + full_house[2] = min; |
| 27 | + return dice == full_house; |
| 28 | +} |
| 29 | + |
| 30 | +int four_of_a_kind_count(const std::array<int, 5>& dice) { |
| 31 | + const int min{dice.at(0)}; |
| 32 | + const int max{dice.at(4)}; |
| 33 | + int count_min{}; |
| 34 | + int count_max{}; |
| 35 | + for (auto element : dice) { |
| 36 | + if (element == min) ++count_min; |
| 37 | + if (element == max) ++count_max; |
| 38 | + } |
| 39 | + if (count_min >= 4) return min * 4; |
| 40 | + if (count_max >= 4) return max * 4; |
| 41 | + return 0; |
| 42 | +} |
| 43 | + |
| 44 | +int score(std::array<int, 5> dice, const std::string& category) { |
| 45 | + if (category == "yacht") { |
| 46 | + return is_yacht(dice) ? 50 : 0; |
| 47 | + } |
| 48 | + if (category == "ones") { |
| 49 | + return simple_scores(dice, 1); |
| 50 | + } |
| 51 | + if (category == "twos") { |
| 52 | + return simple_scores(dice, 2); |
| 53 | + } |
| 54 | + if (category == "threes") { |
| 55 | + return simple_scores(dice, 3); |
| 56 | + } |
| 57 | + if (category == "fours") { |
| 58 | + return simple_scores(dice, 4); |
| 59 | + } |
| 60 | + if (category == "fives") { |
| 61 | + return simple_scores(dice, 5); |
| 62 | + } |
| 63 | + if (category == "sixes") { |
| 64 | + return simple_scores(dice, 6); |
| 65 | + } |
| 66 | + // the rest of the categories can make use of a sorted array: |
| 67 | + std::sort(dice.begin(), dice.end()); |
| 68 | + if (category == "four of a kind") { |
| 69 | + return four_of_a_kind_count(dice); |
| 70 | + } |
| 71 | + if (category == "little straight") { |
| 72 | + std::array<int, 5> little_straight{1, 2, 3, 4, 5}; |
| 73 | + return dice == little_straight ? 30 : 0; |
| 74 | + } |
| 75 | + if (category == "big straight") { |
| 76 | + std::array<int, 5> big_straight{2, 3, 4, 5, 6}; |
| 77 | + return dice == big_straight ? 30 : 0; |
| 78 | + } |
| 79 | + // the last two categories need the sum of the dice: |
| 80 | + int total = std::reduce(dice.begin(), dice.end()); |
| 81 | + if (category == "full house") { |
| 82 | + return is_full_house(dice) ? total : 0; |
| 83 | + } |
| 84 | + if (category == "choice") { |
| 85 | + return total; |
| 86 | + } |
| 87 | + throw std::invalid_argument("unknown category: " + category); |
| 88 | +} |
| 89 | +} // namespace yacht |
0 commit comments