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

@@ -7,8 +7,10 @@
#include <regex>
#include <string>
struct Classifier {
void add_ranges(std::string const &s) {
struct Classifier
{
void add_ranges(std::string const& s)
{
static const std::regex re("(.+): (\\d+)-(\\d+) or (\\d+)-(\\d+)");
std::smatch m;
if (std::regex_search(s, m, re)) {
@@ -22,8 +24,8 @@ struct Classifier {
}
}
void add_range(std::string const &lows, std::string const &highs,
std::size_t field) {
void add_range(std::string const& lows, std::string const& highs, std::size_t field)
{
int low = std::stoi(lows);
int high = std::stoi(highs);
++high;
@@ -39,13 +41,14 @@ struct Classifier {
}
}
void close() {
void close()
{
idx_to_valid_field_.resize(0);
idx_to_valid_field_.resize(field_names_.size(),
(1 << field_names_.size()) - 1);
idx_to_valid_field_.resize(field_names_.size(), (1 << field_names_.size()) - 1);
}
int count_errors(std::string const &s) const {
int count_errors(std::string const& s) const
{
std::string::size_type pos = 0;
int result = 0;
while (pos < s.size()) {
@@ -64,14 +67,14 @@ struct Classifier {
return result;
}
void mark_valid(std::string const &s) {
void mark_valid(std::string const& s)
{
std::string::size_type pos = 0;
unsigned idx = 0;
while (pos < s.size()) {
std::size_t len = 0;
int num = std::stoi(s.substr(pos), &len);
unsigned valid_fields =
num >= num_to_valid_field_.size() ? 0 : num_to_valid_field_[num];
unsigned valid_fields = num >= num_to_valid_field_.size() ? 0 : num_to_valid_field_[num];
idx_to_valid_field_[idx] &= valid_fields;
pos += len;
while (pos < s.size() && s[pos] == ',') {
@@ -82,12 +85,12 @@ struct Classifier {
}
}
void print_valid() const {
void print_valid() const
{
for (std::size_t idx = 0; idx < idx_to_valid_field_.size(); ++idx) {
std::cout << "Index " << idx << " valid for fields:";
unsigned field = 0;
for (auto valid_fields = idx_to_valid_field_[idx]; valid_fields != 0;
valid_fields >>= 1) {
for (auto valid_fields = idx_to_valid_field_[idx]; valid_fields != 0; valid_fields >>= 1) {
if (valid_fields & 1) {
std::cout << " " << field_names_[field];
}
@@ -97,15 +100,15 @@ struct Classifier {
}
}
void reduce() {
void reduce()
{
bool changed = true;
while (changed) {
changed = false;
for (unsigned idx = 0; idx < idx_to_valid_field_.size(); ++idx) {
auto valid_fields = idx_to_valid_field_[idx];
if ((valid_fields & (valid_fields - 1)) == 0) {
std::cout << "Index " << idx << " can only be field " << valid_fields
<< "\n";
std::cout << "Index " << idx << " can only be field " << valid_fields << "\n";
for (unsigned idx2 = 0; idx2 < idx_to_valid_field_.size(); ++idx2) {
if (idx == idx2) {
continue;
@@ -118,14 +121,15 @@ struct Classifier {
}
}
unsigned long calculate_product(std::string const &s,
std::string const &prefix) {
unsigned long calculate_product(std::string const& s, std::string const& prefix)
{
std::vector<unsigned> values;
std::size_t pos = 0;
while (pos < s.size()) {
if (s[pos] == ',') {
++pos;
} else {
}
else {
std::size_t len;
values.push_back(std::stoi(s.substr(pos), &len));
pos += len;
@@ -136,8 +140,7 @@ struct Classifier {
for (unsigned field = 0; field < field_names_.size(); ++field) {
if (field_names_[field].substr(0, prefix.size()) == prefix) {
unsigned idx = 0;
while (idx < idx_to_valid_field_.size() &&
idx_to_valid_field_[idx] != (1 << field)) {
while (idx < idx_to_valid_field_.size() && idx_to_valid_field_[idx] != (1 << field)) {
++idx;
}
assert(idx < idx_to_valid_field_.size());
@@ -155,7 +158,8 @@ struct Classifier {
enum class State { Permitted, Your, Nearby };
int main(int argc, char **argv) {
int main(int argc, char** argv)
{
std::string line;
State s = State::Permitted;
@@ -165,16 +169,21 @@ int main(int argc, char **argv) {
while (std::getline(std::cin, line)) {
if (line == "your ticket:") {
s = State::Your;
} else if (line == "nearby tickets:") {
}
else if (line == "nearby tickets:") {
s = State::Nearby;
c.close();
} else if (line == "") {
}
else if (line == "") {
/* nothing */;
} else if (s == State::Permitted) {
}
else if (s == State::Permitted) {
c.add_ranges(line);
} else if (s == State::Your) {
}
else if (s == State::Your) {
our_ticket = line;
} else if (s == State::Nearby) {
}
else if (s == State::Nearby) {
auto e2 = c.count_errors(line);
e += e2;
if (e2 == 0) {
@@ -187,7 +196,6 @@ int main(int argc, char **argv) {
c.reduce();
c.print_valid();
std::cout << "Product: " << c.calculate_product(our_ticket, "departure ")
<< "\n";
std::cout << "Product: " << c.calculate_product(our_ticket, "departure ") << "\n";
return 0;
}