Skip to content

Commit 0c1fd90

Browse files
authored
Greed is good (#9)
* Solve 'Greed is Good' kata * Refactoring code * Refactoring code (2) * Refactoring code (3) * Refactoring code (4) * Refactoring code (5) * Refactoring code (6)
1 parent 1ff6334 commit 0c1fd90

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

include/greed_is_good/score.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef CPP_KATAS_SCORE_H
2+
#define CPP_KATAS_SCORE_H
3+
4+
#include <vector>
5+
6+
int score(const std::vector<int> &dice);
7+
8+
#endif //CPP_KATAS_SCORE_H

src/greed_is_good/score.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}

test/greed_is_good/score_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <igloo/igloo_alt.h>
2+
#include "directions_reduction/dir_reduction.h"
3+
#include "greed_is_good/score.h"
4+
5+
using namespace igloo;
6+
7+
Describe(GreedisGood_test) {
8+
It(Sample_Test) {
9+
Assert::That(score({2, 3, 4, 6, 2}), Equals(0));
10+
Assert::That(score({2, 4, 4, 5, 4}), Equals(450));
11+
}
12+
};

0 commit comments

Comments
 (0)