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

@@ -10,10 +10,11 @@ enum class Action { TurnOn, Toggle, TurnOff };
using Point = std::pair<unsigned, unsigned>;
/// A command
struct Command {
Command(std::string const &s) {
const char *re =
"(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)";
struct Command
{
Command(std::string const& s)
{
const char* re = "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)";
std::smatch m;
if (!std::regex_search(s, m, std::regex(re))) {
std::cerr << "Unable to interpret:" << s << "\n";
@@ -21,11 +22,14 @@ struct Command {
}
if (m.str(1) == std::string("turn on")) {
act_ = Action::TurnOn;
} else if (m.str(1) == std::string("turn off")) {
}
else if (m.str(1) == std::string("turn off")) {
act_ = Action::TurnOff;
} else if (m.str(1) == std::string("toggle")) {
}
else if (m.str(1) == std::string("toggle")) {
act_ = Action::Toggle;
} else {
}
else {
assert(false);
}
bottom_left_.first = std::stoul(m.str(2), nullptr, 10);
@@ -40,8 +44,11 @@ struct Command {
};
/// Array of lights
template <unsigned N> struct Array {
Array() noexcept {
template<unsigned N>
struct Array
{
Array() noexcept
{
for (unsigned i = 0; i < N; ++i) {
for (unsigned j = 0; j < N; ++j) {
lights_[i][j] = false;
@@ -50,16 +57,15 @@ template <unsigned N> struct Array {
}
/// Apply a command
void apply(Command const &command) noexcept {
void apply(Command const& command) noexcept
{
assert(command.bottom_left_.first < N);
assert(command.bottom_left_.second < N);
assert(command.top_right_.first < N);
assert(command.top_right_.second < N);
for (unsigned i = command.bottom_left_.first; i <= command.top_right_.first;
++i) {
for (unsigned j = command.bottom_left_.second;
j <= command.top_right_.second; ++j) {
for (unsigned i = command.bottom_left_.first; i <= command.top_right_.first; ++i) {
for (unsigned j = command.bottom_left_.second; j <= command.top_right_.second; ++j) {
switch (command.act_) {
case Action::TurnOn:
lights_[i][j] = true;
@@ -76,7 +82,8 @@ template <unsigned N> struct Array {
}
/// How many lights are on
unsigned num_on() const noexcept {
unsigned num_on() const noexcept
{
unsigned count = 0;
for (unsigned i = 0; i < N; ++i) {
for (unsigned j = 0; j < N; ++j) {
@@ -88,7 +95,8 @@ template <unsigned N> struct Array {
}
/// Output a bitmap
void bitmap() const {
void bitmap() const
{
std::cout << "P1\n" << N << " " << N << "\n";
for (unsigned i = 0; i < N; ++i) {
for (unsigned j = 0; j < N; ++j) {
@@ -104,7 +112,8 @@ template <unsigned N> struct Array {
bool lights_[N][N];
};
int main(int argc, char **argv) {
int main(int argc, char** argv)
{
Array<1000> arr;
for (std::string line; std::getline(std::cin, line);) {
Command cmd(line);