Add .clang-format and apply it

This commit is contained in:
2021-12-02 07:18:16 +00:00
parent e58dede1b6
commit cd5e2538df
103 changed files with 2714 additions and 2132 deletions

View File

@@ -12,8 +12,10 @@
using Score = long;
using PropertyMap = std::map<std::string, Score>;
struct Ingredient {
explicit Ingredient(std::string const &s) {
struct Ingredient
{
explicit Ingredient(std::string const& s)
{
auto colon = s.find(':');
name_ = s.substr(0, colon);
auto suffix = s.substr(colon + 1);
@@ -22,25 +24,24 @@ struct Ingredient {
static const std::regex re(",? (\\w+) (-?\\d+)");
std::smatch m;
if (std::regex_search(suffix, m, re)) {
auto [it, success] =
properties_.insert({m.str(1), std::stol(m.str(2))});
auto [it, success] = properties_.insert({m.str(1), std::stol(m.str(2))});
assert(success);
std::cout << " " << it->first << ": " << it->second << "\n";
suffix = m.suffix();
} else {
}
else {
assert(false);
}
}
}
bool operator<(Ingredient const &rhs) const noexcept {
return name_ < rhs.name_;
}
bool operator<(Ingredient const& rhs) const noexcept { return name_ < rhs.name_; }
std::string const &name() const noexcept { return name_; }
std::string const& name() const noexcept { return name_; }
void update_score(Score amount, PropertyMap &totals) const {
for (auto const &kv : properties_) {
void update_score(Score amount, PropertyMap& totals) const
{
for (auto const& kv : properties_) {
if (kv.first == "calories") {
continue;
}
@@ -56,20 +57,20 @@ private:
PropertyMap properties_;
};
struct Ingredients {
void add_ingredient(std::string const &s) {
ingredients_.push_back(Ingredient(s));
}
struct Ingredients
{
void add_ingredient(std::string const& s) { ingredients_.push_back(Ingredient(s)); }
Score best_combination(Score amount) const {
Score best_combination(Score amount) const
{
PropertyMap totals;
return best_combination(amount, ingredients_.begin(), 0UL, totals);
}
private:
Score best_combination(Score amount,
std::vector<Ingredient>::const_iterator it,
Score best_score, PropertyMap &totals) const {
Score best_combination(Score amount, std::vector<Ingredient>::const_iterator it, Score best_score,
PropertyMap& totals) const
{
it->update_score(amount, totals);
auto it2 = it;
++it2;
@@ -83,17 +84,17 @@ private:
for (auto allocation = amount - 1; allocation > 0; --allocation) {
it->update_score(-1, totals);
best_score =
std::max(best_score, best_combination(amount - allocation, it2,
best_score, totals));
std::max(best_score, best_combination(amount - allocation, it2, best_score, totals));
}
it->update_score(-1, totals);
return best_score;
}
Score calculate_score(PropertyMap const &totals) const {
Score calculate_score(PropertyMap const& totals) const
{
Score r = 1;
for (auto const &kv : totals) {
for (auto const& kv : totals) {
if (kv.first == "calories") {
continue;
}
@@ -109,7 +110,8 @@ private:
std::vector<Ingredient> ingredients_;
};
int main(int argc, char **argv) {
int main(int argc, char** argv)
{
Ingredients ingredients;
std::string line;