|
| 1 | +#include <utility> |
| 2 | +#include <vector> |
| 3 | +#include <algorithm> |
| 4 | + |
| 5 | +/* |
| 6 | + * https://www.codewars.com/kata/5270d0d18625160ada0000e4/train/cpp |
| 7 | + */ |
| 8 | + |
| 9 | +class Rule { |
| 10 | +public: |
| 11 | + virtual bool matches(const std::vector<int> &dice); |
| 12 | + |
| 13 | + virtual int points() { return 0; }; |
| 14 | + |
| 15 | + virtual std::vector<int> remaining(const std::vector<int> &dice); |
| 16 | +}; |
| 17 | + |
| 18 | +class Greed { |
| 19 | +private: |
| 20 | + const std::vector<int> _dice; |
| 21 | + |
| 22 | + Rule *matching_rule(); |
| 23 | + |
| 24 | + explicit Greed(std::vector<int> dice) : _dice(std::move(dice)) {} |
| 25 | + |
| 26 | +public: |
| 27 | + int score(); |
| 28 | + |
| 29 | + static Greed from(const std::vector<int> &dice); |
| 30 | +}; |
| 31 | + |
| 32 | +int score(const std::vector<int> &dice) { |
| 33 | + return Greed::from(dice).score(); |
| 34 | +} |
| 35 | + |
| 36 | +class Three : public Rule { |
| 37 | +private: |
| 38 | + const int _value; |
| 39 | + const int _points; |
| 40 | + |
| 41 | +public: |
| 42 | + Three(int value, int point) : _value(value), _points(point) {} |
| 43 | + |
| 44 | + bool matches(const std::vector<int> &dice) override; |
| 45 | + |
| 46 | + int points() override { return _points; }; |
| 47 | + |
| 48 | + std::vector<int> remaining(const std::vector<int> &dice) override; |
| 49 | +}; |
| 50 | + |
| 51 | +class One : public Rule { |
| 52 | +private: |
| 53 | + const int _value; |
| 54 | + const int _points; |
| 55 | + |
| 56 | +public: |
| 57 | + One(int value, int point) : _value(value), _points(point) {} |
| 58 | + |
| 59 | + bool matches(const std::vector<int> &dice) override; |
| 60 | + |
| 61 | + int points() override { return _points; }; |
| 62 | +}; |
| 63 | + |
| 64 | +bool Rule::matches(const std::vector<int> &dice) { |
| 65 | + return true; |
| 66 | +}; |
| 67 | + |
| 68 | +std::vector<int> Rule::remaining(const std::vector<int> &dice) { |
| 69 | + std::vector<int> copy; |
| 70 | + copy = dice; |
| 71 | + copy.erase(copy.cbegin()); |
| 72 | + |
| 73 | + return copy; |
| 74 | +} |
| 75 | + |
| 76 | +bool Three::matches(const std::vector<int> &dice) { |
| 77 | + return dice.size() >= 3 && |
| 78 | + dice.at(0) == _value && |
| 79 | + dice.at(1) == _value && |
| 80 | + dice.at(2) == _value; |
| 81 | +}; |
| 82 | + |
| 83 | +std::vector<int> Three::remaining(const std::vector<int> &dice) { |
| 84 | + std::vector<int> copy = dice; |
| 85 | + copy.erase(copy.cbegin()); |
| 86 | + copy.erase(copy.cbegin()); |
| 87 | + copy.erase(copy.cbegin()); |
| 88 | + |
| 89 | + return copy; |
| 90 | +} |
| 91 | + |
| 92 | +bool One::matches(const std::vector<int> &dice) { |
| 93 | + return !dice.empty() && |
| 94 | + dice.at(0) == _value; |
| 95 | +}; |
| 96 | + |
| 97 | +const std::vector<Rule *> rules = std::vector<Rule *>{ |
| 98 | + new Three(1, 1000), |
| 99 | + new Three(6, 600), |
| 100 | + new Three(5, 500), |
| 101 | + new Three(4, 400), |
| 102 | + new Three(3, 300), |
| 103 | + new Three(2, 200), |
| 104 | + new One(1, 100), |
| 105 | + new One(5, 50), |
| 106 | + new Rule() |
| 107 | +}; |
| 108 | + |
| 109 | +Rule *Greed::matching_rule() { |
| 110 | + for (Rule *rule: rules) { |
| 111 | + if (rule->matches(_dice)) { |
| 112 | + return rule; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + throw std::exception(); |
| 117 | +} |
| 118 | + |
| 119 | +int Greed::score() { |
| 120 | + if (_dice.empty()) { |
| 121 | + return 0; |
| 122 | + } |
| 123 | + |
| 124 | + Rule *rule = matching_rule(); |
| 125 | + auto *greed = new Greed(rule->remaining(_dice)); |
| 126 | + |
| 127 | + return rule->points() + greed->score(); |
| 128 | +} |
| 129 | + |
| 130 | +Greed Greed::from(const std::vector<int> &dice) { |
| 131 | + std::vector<int> copy = dice; |
| 132 | + std::sort(copy.begin(), copy.end()); |
| 133 | + |
| 134 | + return Greed(copy); |
| 135 | +} |
0 commit comments