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

@@ -19,9 +19,11 @@ using IngredientInfo = std::pair<unsigned, Allergens>;
using IngredientMap = std::map<Ingredient, IngredientInfo>;
using AllergenMap = std::map<Allergen, Ingredients>;
class IngredientParser {
class IngredientParser
{
public:
void add_recipe(std::string const &s) {
void add_recipe(std::string const& s)
{
auto it = s.begin();
Ingredients i;
while (it != s.end()) {
@@ -33,10 +35,10 @@ public:
it = ite;
if (ingredient == "(contains") {
break;
} else {
}
else {
i.insert(ingredient);
auto [iit, success] =
ingredients_.insert({ingredient, {1, Allergens()}});
auto [iit, success] = ingredients_.insert({ingredient, {1, Allergens()}});
if (!success) {
iit->second.first++;
}
@@ -44,8 +46,7 @@ public:
}
while (it != s.end()) {
auto ite = std::find_if(
it, s.end(), [](char c) -> bool { return c == ',' || c == ')'; });
auto ite = std::find_if(it, s.end(), [](char c) -> bool { return c == ',' || c == ')'; });
auto allergen = std::string(it, ite);
++ite;
while (ite != s.end() && *ite == ' ') {
@@ -56,18 +57,18 @@ public:
if (!success) {
Ingredients a;
std::set_intersection(i.begin(), i.end(), insert_it->second.begin(),
insert_it->second.end(),
std::inserter(a, a.end()));
insert_it->second.end(), std::inserter(a, a.end()));
insert_it->second = a;
}
}
}
unsigned clean_ingredients() {
for (auto const &kv : allergens_) {
unsigned clean_ingredients()
{
for (auto const& kv : allergens_) {
auto allergen = kv.first;
std::cout << "Allergen " << allergen << ":";
for (auto const &i : kv.second) {
for (auto const& i : kv.second) {
std::cout << " " << i;
auto it = ingredients_.find(i);
assert(it != ingredients_.end());
@@ -77,10 +78,9 @@ public:
}
unsigned count = 0;
for (auto const &i : ingredients_) {
for (auto const& i : ingredients_) {
if (i.second.second.size() == 0) {
std::cout << i.first << " is not an allergen, appears "
<< i.second.first << ".\n";
std::cout << i.first << " is not an allergen, appears " << i.second.first << ".\n";
count += i.second.first;
}
}
@@ -88,23 +88,23 @@ public:
return count;
}
std::string dangerous_ingredients() {
std::string dangerous_ingredients()
{
bool changed = true;
Ingredients i;
while (changed) {
changed = false;
for (auto const &kv : allergens_) {
for (auto const& kv : allergens_) {
if (kv.second.size() == 1) {
auto it = kv.second.begin();
Ingredient const &ing = *it;
Ingredient const& ing = *it;
auto [iit, success] = i.insert(ing);
if (!success) {
continue;
}
changed = true;
std::cout << "Allergen " << kv.first << " is in ingredient " << ing
<< "\n";
for (auto &kv2 : allergens_) {
std::cout << "Allergen " << kv.first << " is in ingredient " << ing << "\n";
for (auto& kv2 : allergens_) {
if (kv2.second.size() != 1) {
kv2.second.erase(ing);
}
@@ -114,7 +114,7 @@ public:
}
std::string result;
for (auto const &kv : allergens_) {
for (auto const& kv : allergens_) {
result += ",";
result += *kv.second.begin();
}
@@ -127,7 +127,8 @@ private:
AllergenMap allergens_;
};
int main(void) {
int main(void)
{
std::string line;
IngredientParser parser;
while (std::getline(std::cin, line)) {