Skip to content

Commit

Permalink
Maintenance (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell authored Dec 18, 2023
2 parents 3453ea3 + 99f7010 commit 30bea55
Show file tree
Hide file tree
Showing 37 changed files with 919 additions and 993 deletions.
35 changes: 35 additions & 0 deletions 2023/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
BasedOnStyle: Microsoft
AccessModifierOffset: -2
AlignAfterOpenBracket: DontAlign
AllowAllParametersOfDeclarationOnNextLine: false
AlignConsecutiveMacros: AcrossEmptyLinesAndComments
AlignConsecutiveAssignments: None
AlignOperands: Align
AlignArrayOfStructures: Left
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: Empty
AllowAllConstructorInitializersOnNextLine: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakConstructorInitializers: AfterColon
ColumnLimit: 100
ContinuationIndentWidth: 2
ConstructorInitializerAllOnOneLineOrOnePerLine: false
Cpp11BracedListStyle: true
PointerAlignment: Left
ExperimentalAutoDetectBinPacking: true
FixNamespaceComments: false
IndentWrappedFunctionNames: true
NamespaceIndentation: None
SortIncludes: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeParens: ControlStatements
SpaceInEmptyBlock: true
StatementAttributeLikeMacros: []
TabWidth: 2
UseCRLF: false
UseTab: Never
AlwaysBreakTemplateDeclarations: Yes
IndentWidth: 2
18 changes: 7 additions & 11 deletions 2023/cmd/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

namespace app {

std::string replace(std::string_view in, char target,
std::string_view replacement) {
std::string replace(std::string_view in, char target, std::string_view replacement) {
std::string out;
out.reserve(in.size());

Expand All @@ -34,27 +33,25 @@ std::string app::usage() {

for (auto cmd : commands_flat) {
s << '\n';
for (auto &alias : cmd.flags) {
for (auto& alias : cmd.flags) {
s << std::format("aoc2023 {}\n", alias);
}

constexpr auto prefix = " ";
std::string help =
prefix + replace(cmd.help, '\n', std::format("\n{}", prefix));
std::string help = prefix + replace(cmd.help, '\n', std::format("\n{}", prefix));
s << help << '\n';
}

return s.str();
}

void app::register_command(command const &cmd) {
void app::register_command(command const& cmd) {
this->commands_flat.push_back(cmd);

for (auto alias : cmd.flags) {
auto it = this->commands.find(alias);
if (it != this->commands.end()) {
throw std::runtime_error(
std::format("Flag {} is already defined", alias));
throw std::runtime_error(std::format("Flag {} is already defined", alias));
}

this->commands[alias] = cmd;
Expand Down Expand Up @@ -82,8 +79,7 @@ int app::run(argv args) {

auto argv_begin = it + 1;
auto argv_end =
std::find_if(argv_begin, args.end(),
[](std::string_view s) { return s.starts_with("-"); });
std::find_if(argv_begin, args.end(), [](std::string_view s) { return s.starts_with("-"); });

xlog::debug("Found command {} with {} arguments", c, argv_end - argv_begin);
cmd.emplace_back(icmd->second, std::span{argv_begin, argv_end});
Expand All @@ -92,7 +88,7 @@ int app::run(argv args) {
}

// Execute commands
for (auto &command : cmd) {
for (auto& command : cmd) {
auto r = command.c.run(*this, command.args);
switch (r) {
case exit_success:
Expand Down
6 changes: 2 additions & 4 deletions 2023/cmd/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ using argv = std::span<std::string_view>;
struct command {
std::vector<std::string_view> flags;
std::string_view help;
std::function<int(app &, argv)> run = [](app &, argv) {
return exit_success;
};
std::function<int(app&, argv)> run = [](app&, argv) { return exit_success; };
};

class app {
public:
void register_command(command const &);
void register_command(command const&);
std::string usage();
int run(argv args);

Expand Down
63 changes: 32 additions & 31 deletions 2023/cmd/cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

namespace app {

std::optional<xmas::solution::duration>
solve_day(std::map<const int, std::unique_ptr<xmas::solution>>::value_type const
&solution,
bool verbose);
std::optional<xmas::solution::duration> solve_day(
std::map<const int, std::unique_ptr<xmas::solution>>::value_type const&
solution,
bool verbose);

solution_vector select_all_days() {
try {
populate_registry();
} catch (std::runtime_error &e) {
} catch (std::runtime_error& e) {
xlog::error("could not populate the registry fully: {}", e.what());
}
const auto &solutions = xmas::registered_solutions();
const auto& solutions = xmas::registered_solutions();

solution_vector days;

Expand All @@ -37,15 +37,15 @@ solution_vector select_all_days() {
solution_vector select_days(argv days) {
try {
populate_registry();
} catch (std::runtime_error &e) {
} catch (std::runtime_error& e) {
xlog::error("could not populate the registry fully: {}", e.what());
}
const auto &solutions = xmas::registered_solutions();
const auto& solutions = xmas::registered_solutions();

solution_vector out;

out.reserve(days.size());
for (auto &day : days) {
for (auto& day : days) {
int d = -1;
std::from_chars(day.begin(), day.end(), d);
if (d == -1) {
Expand All @@ -65,7 +65,7 @@ solution_vector select_days(argv days) {
return out;
}

bool execute_days(app &, solution_vector const &days) {
bool execute_days(app&, solution_vector const& days) {
xmas::solution::duration total{};
bool total_success = true;

Expand All @@ -80,9 +80,8 @@ bool execute_days(app &, solution_vector const &days) {
}

xlog::info("DONE");
xlog::info(
"Total time was {} ms",
std::chrono::duration_cast<std::chrono::milliseconds>(total).count());
xlog::info("Total time was {} ms",
std::chrono::duration_cast<std::chrono::milliseconds>(total).count());

return total_success;
}
Expand All @@ -91,16 +90,17 @@ std::int64_t microseconds(xmas::solution::duration d) {
return std::chrono::duration_cast<std::chrono::microseconds>(d).count();
}

bool time_days(app &, solution_vector const &days,
xmas::solution::duration timeout) {
bool time_days(app&,
solution_vector const& days,
xmas::solution::duration timeout) {
xmas::solution::duration total{};
bool total_success = true;

xlog::debug("Timing solution in debug mode is not recommended");

xlog::info("Every day's solution will be executed non-stop for {} seconds to "
"evaluate its performance.",
std::chrono::duration_cast<std::chrono::seconds>(timeout).count());
std::chrono::duration_cast<std::chrono::seconds>(timeout).count());

constexpr std::string_view fmt = "{:>7} {:>8} {:>7} {:>7}";
xlog::info("");
Expand Down Expand Up @@ -138,9 +138,9 @@ bool time_days(app &, solution_vector const &days,
}

// Average
const auto mean = std::chrono::duration_cast<std::chrono::microseconds>(
daily_total / iter)
.count();
const auto mean =
std::chrono::duration_cast<std::chrono::microseconds>(daily_total / iter)
.count();
const auto dev = static_cast<std::int64_t>(std::sqrt(S / iter));

// Report
Expand All @@ -149,25 +149,26 @@ bool time_days(app &, solution_vector const &days,
total += daily_total / iter;
}

xlog::info(
fmt, "TOTAL", "-",
std::chrono::duration_cast<std::chrono::microseconds>(total).count(),
"-");
xlog::info(fmt,
"TOTAL",
"-",
std::chrono::duration_cast<std::chrono::microseconds>(total).count(),
"-");

return total_success;
}

std::optional<xmas::solution::duration>
solve_day(std::map<const int, std::unique_ptr<xmas::solution>>::value_type const
&solution,
bool verbose) {
std::optional<xmas::solution::duration> solve_day(
std::map<const int, std::unique_ptr<xmas::solution>>::value_type const&
solution,
bool verbose) {

try {
solution.second->set_input(
std::format("./data/{:02d}/input.txt", solution.second->day()));
} catch (std::runtime_error &e) {
xlog::error("day {} could not load: {}\n", solution.second->day(),
e.what());
std::format("./data/{:02d}/input.txt", solution.second->day()));
} catch (std::runtime_error& e) {
xlog::error(
"day {} could not load: {}\n", solution.second->day(), e.what());
return {};
}

Expand Down
Loading

0 comments on commit 30bea55

Please sign in to comment.