From 9ecc3ff53de8889823fc09b020d65b9d410aae7d Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 2 Dec 2021 19:57:58 +0000 Subject: [PATCH] Update some 2015 sources to .clang-tidy --- .clang-tidy | 116 ++++++++++++++++++++++--------------------- 2015/puzzle-01-01.cc | 7 +-- 2015/puzzle-01-02.cc | 7 +-- 2015/puzzle-02-01.cc | 20 ++++---- 2015/puzzle-02-02.cc | 30 +++++------ 2015/puzzle-03-01.cc | 10 ++-- 2015/puzzle-03-02.cc | 24 ++++----- 2015/puzzle-04-01.cc | 22 ++++---- 2015/puzzle-04-02.cc | 22 ++++---- 2015/puzzle-05-01.cc | 6 +-- 2015/puzzle-05-02.cc | 12 ++--- 2015/puzzle-06-01.cc | 26 +++++----- 2015/puzzle-06-02.cc | 30 +++++------ 2015/puzzle-07-01.cc | 93 ++++++++++++++++++---------------- 2015/puzzle-07-02.cc | 84 ++++++++++++++++--------------- 2015/puzzle-08-01.cc | 9 ++-- 2015/puzzle-08-02.cc | 9 +--- 2015/puzzle-09-01.cc | 7 ++- 2015/puzzle-09-02.cc | 9 ++-- 2015/puzzle-10-01.cc | 9 +--- 2015/puzzle-10-02.cc | 9 +--- 2015/puzzle-11-01.cc | 12 ++--- 2015/puzzle-11-02.cc | 12 ++--- 2015/puzzle-12-01.cc | 8 +-- 2015/puzzle-12-02.cc | 18 ++----- 2020/puzzle-08-01.cc | 10 ++-- 2020/puzzle-08-02.cc | 20 ++++---- 27 files changed, 305 insertions(+), 336 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 410728e..b43995d 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -8,65 +8,69 @@ # misc-no-recursion: We do not mind recursion (and we need to recurse) # readability-function-cognitive-complexity: Too many asserts trigger this one unnecessarily # cppcoreguidelines-pro-bounds-array-to-pointer-decay: Triggers on assert() -Checks: >- - *, - -abseil-*, - -altera-*, - -android-*, - -fuchsia-*, - -hicpp-*, - -llvmlibc-*, - -objc-*, - -zircon-*, - -cppcoreguidelines-owning-memory, - -cppcoreguidelines-pro-type-vararg, - -google-runtime-references, - -llvm-header-guard, - -misc-no-recursion, - -readability-function-cognitive-complexity, - -clang-diagnostic-ignored-optimization-argument, - -cppcoreguidelines-pro-bounds-array-to-pointer-decay +Checks: >- + *, + -abseil-*, + -altera-*, + -android-*, + -fuchsia-*, + -hicpp-*, + -llvmlibc-*, + -objc-*, + -zircon-*, + -cppcoreguidelines-owning-memory, + -cppcoreguidelines-pro-type-vararg, + -google-runtime-references, + -llvm-header-guard, + -misc-no-recursion, + -readability-function-cognitive-complexity, + -clang-diagnostic-ignored-optimization-argument, + -cppcoreguidelines-pro-bounds-array-to-pointer-decay, + -readability-identifier-length, + -cppcoreguidelines-avoid-magic-numbers, + -readability-magic-numbers, + -misc-non-private-member-variables-in-classes WarningsAsErrors: '*,-clang-diagnostic-ignored-optimization-argument' HeaderFilterRegex: '' AnalyzeTemporaryDtors: false -FormatStyle: file -User: mgrettondann +FormatStyle: file +User: mgrettondann CheckOptions: - - key: llvm-else-after-return.WarnOnConditionVariables - value: '0' - - key: modernize-loop-convert.MinConfidence - value: reasonable - - key: modernize-replace-auto-ptr.IncludeStyle - value: llvm - - key: cert-str34-c.DiagnoseSignedUnsignedCharComparisons - value: '0' - - key: google-readability-namespace-comments.ShortNamespaceLines - value: '10' - - key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField - value: '0' - - key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic - value: '1' - - key: cert-dcl16-c.NewSuffixes - value: 'L;LL;LU;LLU' - - key: google-readability-braces-around-statements.ShortStatementLines - value: '1' - - key: modernize-pass-by-value.IncludeStyle - value: llvm - - key: google-readability-namespace-comments.SpacesBeforeComments - value: '2' - - key: modernize-loop-convert.MaxCopySize - value: '16' - - key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors - value: '1' - - key: modernize-use-nullptr.NullMacros - value: 'NULL' - - key: llvm-qualified-auto.AddConstToQualified - value: '0' - - key: modernize-loop-convert.NamingStyle - value: CamelCase - - key: llvm-else-after-return.WarnOnUnfixable - value: '0' - - key: google-readability-function-size.StatementThreshold - value: '800' + - key: llvm-else-after-return.WarnOnConditionVariables + value: '0' + - key: modernize-loop-convert.MinConfidence + value: reasonable + - key: modernize-replace-auto-ptr.IncludeStyle + value: llvm + - key: cert-str34-c.DiagnoseSignedUnsignedCharComparisons + value: '0' + - key: google-readability-namespace-comments.ShortNamespaceLines + value: '10' + - key: cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField + value: '0' + - key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic + value: '1' + - key: cert-dcl16-c.NewSuffixes + value: 'L;LL;LU;LLU' + - key: google-readability-braces-around-statements.ShortStatementLines + value: '1' + - key: modernize-pass-by-value.IncludeStyle + value: llvm + - key: google-readability-namespace-comments.SpacesBeforeComments + value: '2' + - key: modernize-loop-convert.MaxCopySize + value: '16' + - key: cppcoreguidelines-explicit-virtual-functions.IgnoreDestructors + value: '1' + - key: modernize-use-nullptr.NullMacros + value: 'NULL' + - key: llvm-qualified-auto.AddConstToQualified + value: '0' + - key: modernize-loop-convert.NamingStyle + value: CamelCase + - key: llvm-else-after-return.WarnOnUnfixable + value: '0' + - key: google-readability-function-size.StatementThreshold + value: '800' ... diff --git a/2015/puzzle-01-01.cc b/2015/puzzle-01-01.cc index b66b9ed..a290655 100644 --- a/2015/puzzle-01-01.cc +++ b/2015/puzzle-01-01.cc @@ -2,13 +2,14 @@ #include #include -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { int floor = 0; - for (auto c : line) { + for (auto c : line) { // NOLINT(readability-identifier-length) assert(c == '(' || c == ')'); - floor += (c == '(') - (c == ')'); + floor += (c == '(') ? 1 : 0; + floor -= (c == ')') ? 1 : 0; } std::cout << floor << "\n"; } diff --git a/2015/puzzle-01-02.cc b/2015/puzzle-01-02.cc index fcafe91..0d07bda 100644 --- a/2015/puzzle-01-02.cc +++ b/2015/puzzle-01-02.cc @@ -2,15 +2,16 @@ #include #include -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { int floor = 0; std::string::size_type pos = 0; - for (auto c : line) { + for (auto c : line) { // NOLINT(readability-identifier-length) ++pos; assert(c == '(' || c == ')'); - floor += (c == '(') - (c == ')'); + floor += (c == '(') ? 1 : 0; + floor -= (c == ')') ? 1 : 0; if (floor == -1) { break; } diff --git a/2015/puzzle-02-01.cc b/2015/puzzle-02-01.cc index df7c0d7..69b89cf 100644 --- a/2015/puzzle-02-01.cc +++ b/2015/puzzle-02-01.cc @@ -8,7 +8,7 @@ struct Box /** Construct box. * \param s String representation of dimensions 'lxwxh' */ - Box(std::string const& s) + explicit Box(std::string const& s) { std::size_t pos = 0; l_ = std::stoul(s, &pos, 10); @@ -22,22 +22,22 @@ struct Box } // How much paper does this box need? - unsigned long paper_needed() const + [[nodiscard]] auto paper_needed() const -> std::uint64_t { - unsigned long s1 = l_ * w_; - unsigned long s2 = w_ * h_; - unsigned long s3 = h_ * l_; + std::uint64_t s1 = l_ * w_; + std::uint64_t s2 = w_ * h_; + std::uint64_t s3 = h_ * l_; return 2 * (s1 + s2 + s3) + std::min({s1, s2, s3}); } - unsigned long l_; - unsigned long w_; - unsigned long h_; + std::uint64_t l_; + std::uint64_t w_; + std::uint64_t h_; }; -int main(int argc, char** argv) +auto main() -> int { - unsigned long total = 0; + std::uint64_t total = 0; for (std::string line; std::getline(std::cin, line);) { Box b(line); auto amount = b.paper_needed(); diff --git a/2015/puzzle-02-02.cc b/2015/puzzle-02-02.cc index d086298..a1644f3 100644 --- a/2015/puzzle-02-02.cc +++ b/2015/puzzle-02-02.cc @@ -6,9 +6,9 @@ struct Box { /** Construct box. - * \param s String representation of dimensions 'lxwxh' + * \param s String representation of dimensions `lxwxh` */ - Box(std::string const& s) + explicit Box(std::string const& s) { std::size_t pos = 0; l_ = std::stoul(s, &pos, 10); @@ -22,33 +22,33 @@ struct Box } // How much paper does this box need? - unsigned long paper_needed() const + [[nodiscard]] auto paper_needed() const -> std::uint64_t { - unsigned long s1 = l_ * w_; - unsigned long s2 = w_ * h_; - unsigned long s3 = h_ * l_; + auto s1{l_ * w_}; + auto s2{w_ * h_}; + auto s3{h_ * l_}; return 2 * (s1 + s2 + s3) + std::min({s1, s2, s3}); } // How much ribbon do we need? - unsigned long ribbon_needed() const + [[nodiscard]] auto ribbon_needed() const -> std::uint64_t { // The various side perimeters - we want the min of these multiplied by // volume. - unsigned long p1 = 2 * (l_ + w_); - unsigned long p2 = 2 * (w_ + h_); - unsigned long p3 = 2 * (h_ + l_); + auto p1{2 * (l_ + w_)}; + auto p2{2 * (w_ + h_)}; + auto p3{2 * (h_ + l_)}; return l_ * w_ * h_ + std::min({p1, p2, p3}); } - unsigned long l_; - unsigned long w_; - unsigned long h_; + std::uint64_t l_; + std::uint64_t w_; + std::uint64_t h_; }; -int main(int argc, char** argv) +auto main() -> int { - unsigned long total = 0; + std::uint64_t total = 0; for (std::string line; std::getline(std::cin, line);) { Box b(line); auto amount = b.ribbon_needed(); diff --git a/2015/puzzle-03-01.cc b/2015/puzzle-03-01.cc index 0276ef1..a91c6d7 100644 --- a/2015/puzzle-03-01.cc +++ b/2015/puzzle-03-01.cc @@ -1,19 +1,17 @@ #include -#include #include #include -#include struct Pos { - Pos(int x, int y) : x_(x), y_(y) {} + Pos(int x, int y) : x_(x), y_(y) {} // NOLINT(bugprone-easily-swappable-parameters) - bool operator<(Pos const& rhs) const noexcept + auto operator<(Pos const& rhs) const noexcept -> bool { return x_ < rhs.x_ || (x_ == rhs.x_ && y_ < rhs.y_); } - bool operator==(Pos const& rhs) const noexcept { return x_ == rhs.x_ && y_ == rhs.y_; } + auto operator==(Pos const& rhs) const noexcept -> bool { return x_ == rhs.x_ && y_ == rhs.y_; } void move(char c) { @@ -40,7 +38,7 @@ struct Pos int y_; }; -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { std::set visited; diff --git a/2015/puzzle-03-02.cc b/2015/puzzle-03-02.cc index e913c08..72c8a46 100644 --- a/2015/puzzle-03-02.cc +++ b/2015/puzzle-03-02.cc @@ -1,19 +1,17 @@ #include -#include #include #include -#include struct Pos { - Pos(int x, int y) : x_(x), y_(y) {} + Pos(int x, int y) : x_(x), y_(y) {} // NOLINT(bugprone-easily-swappable-parameters) - bool operator<(Pos const& rhs) const noexcept + auto operator<(Pos const& rhs) const noexcept -> bool { return x_ < rhs.x_ || (x_ == rhs.x_ && y_ < rhs.y_); } - bool operator==(Pos const& rhs) const noexcept { return x_ == rhs.x_ && y_ == rhs.y_; } + auto operator==(Pos const& rhs) const noexcept -> bool { return x_ == rhs.x_ && y_ == rhs.y_; } void move(char c) noexcept { @@ -40,25 +38,25 @@ struct Pos int y_; }; -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { std::set visited; Pos santa(0, 0); - Pos robo_santa(0, 0); + Pos robot_santa(0, 0); visited.insert(santa); - visited.insert(robo_santa); - bool do_robo = false; + visited.insert(robot_santa); + bool do_robot = false; for (auto c : line) { - if (do_robo) { - robo_santa.move(c); - visited.insert(robo_santa); + if (do_robot) { + robot_santa.move(c); + visited.insert(robot_santa); } else { santa.move(c); visited.insert(santa); } - do_robo = !do_robo; + do_robot = !do_robot; } std::cout << "Houses visited = " << visited.size() << "\n"; } diff --git a/2015/puzzle-04-01.cc b/2015/puzzle-04-01.cc index 972c0a6..925edbf 100644 --- a/2015/puzzle-04-01.cc +++ b/2015/puzzle-04-01.cc @@ -1,27 +1,27 @@ #include +#include #include #include -#include #include #include -using MD5Digest = unsigned char[EVP_MAX_MD_SIZE]; +using MD5Digest = std::array; -unsigned int md5(MD5Digest digest, std::string const& s) +auto md5(MD5Digest& digest, std::string const& s) -> unsigned int { EVP_MD const* md{EVP_md5()}; - unsigned int md_len; + unsigned int md_len{0}; - EVP_MD_CTX* md_ctxt{EVP_MD_CTX_new()}; - assert(md_ctxt != NULL); - EVP_DigestInit_ex2(md_ctxt, md, NULL); - EVP_DigestUpdate(md_ctxt, s.data(), s.length()); - EVP_DigestFinal_ex(md_ctxt, digest, &md_len); + EVP_MD_CTX* md_context{EVP_MD_CTX_new()}; + assert(md_context != nullptr); + EVP_DigestInit_ex2(md_context, md, nullptr); + EVP_DigestUpdate(md_context, s.data(), s.length()); + EVP_DigestFinal_ex(md_context, digest.data(), &md_len); return md_len; } -bool is_valid(std::string const& s) +auto is_valid(std::string const& s) -> bool { MD5Digest digest; auto len = md5(digest, s); @@ -29,7 +29,7 @@ bool is_valid(std::string const& s) return digest[0] == 0 && digest[1] == 0 && (digest[2] & 0xf0) == 0; } -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { unsigned i = 0; diff --git a/2015/puzzle-04-02.cc b/2015/puzzle-04-02.cc index b4847a5..eb5cdd7 100644 --- a/2015/puzzle-04-02.cc +++ b/2015/puzzle-04-02.cc @@ -1,27 +1,27 @@ #include +#include #include #include -#include #include #include -using MD5Digest = unsigned char[EVP_MAX_MD_SIZE]; +using MD5Digest = std::array; -unsigned int md5(MD5Digest digest, std::string const& s) +auto md5(MD5Digest& digest, std::string const& s) -> unsigned int { EVP_MD const* md{EVP_md5()}; - unsigned int md_len; + unsigned int md_len{0}; - EVP_MD_CTX* md_ctxt{EVP_MD_CTX_new()}; - assert(md_ctxt != NULL); - EVP_DigestInit_ex2(md_ctxt, md, NULL); - EVP_DigestUpdate(md_ctxt, s.data(), s.length()); - EVP_DigestFinal_ex(md_ctxt, digest, &md_len); + EVP_MD_CTX* md_context{EVP_MD_CTX_new()}; + assert(md_context != nullptr); + EVP_DigestInit_ex2(md_context, md, nullptr); + EVP_DigestUpdate(md_context, s.data(), s.length()); + EVP_DigestFinal_ex(md_context, digest.data(), &md_len); return md_len; } -bool is_valid(std::string const& s) +auto is_valid(std::string const& s) -> bool { MD5Digest digest; auto len = md5(digest, s); @@ -29,7 +29,7 @@ bool is_valid(std::string const& s) return digest[0] == 0 && digest[1] == 0 && digest[2] == 0; } -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { unsigned i = 0; diff --git a/2015/puzzle-05-01.cc b/2015/puzzle-05-01.cc index c11473d..ee44bd8 100644 --- a/2015/puzzle-05-01.cc +++ b/2015/puzzle-05-01.cc @@ -1,7 +1,5 @@ #include -#include #include -#include #include // Check if a string is nice: @@ -9,7 +7,7 @@ // >=3 vowels // At least one double letter // No instances of 'ab', 'cd', 'pq', or 'xy'. -bool is_nice(std::string const& s) noexcept +auto is_nice(std::string const& s) noexcept -> bool { unsigned vowel_count = 0; bool repeated = false; @@ -33,7 +31,7 @@ bool is_nice(std::string const& s) noexcept return repeated && vowel_count >= 3; } -int main(int argc, char** argv) +auto main() -> int { unsigned nice_strings; for (std::string line; std::getline(std::cin, line);) { diff --git a/2015/puzzle-05-02.cc b/2015/puzzle-05-02.cc index 780e2fa..6baf0bc 100644 --- a/2015/puzzle-05-02.cc +++ b/2015/puzzle-05-02.cc @@ -1,21 +1,19 @@ #include -#include #include -#include #include // Check if a string is nice: // Nice strings have: // repeated double letters - but not overlapping // repeated letters separated by one other. -bool is_nice(std::string const& s) noexcept +auto is_nice(std::string const& s) noexcept -> bool { bool repeated_pair = false; bool repeated_sep = false; std::cout << s; for (std::string::size_type i = 0; i < s.length() - 2; ++i) { - // Find whether the two characters starting at i are repeated in the rest of + // Find whether the two characters starting at `i` are repeated in the rest of // the string. We don't have to look backwards as we will already have // scanned it. if (s.substr(i + 2).find(s.substr(i, 2)) != std::string::npos) { @@ -35,11 +33,11 @@ bool is_nice(std::string const& s) noexcept return repeated_pair && repeated_sep; } -int main(int argc, char** argv) +auto main() -> int { - unsigned nice_strings; + unsigned nice_strings{0}; for (std::string line; std::getline(std::cin, line);) { - nice_strings += is_nice(line); + nice_strings += is_nice(line) ? 1 : 0; } std::cout << nice_strings << '\n'; diff --git a/2015/puzzle-06-01.cc b/2015/puzzle-06-01.cc index 6729df3..484621d 100644 --- a/2015/puzzle-06-01.cc +++ b/2015/puzzle-06-01.cc @@ -2,7 +2,6 @@ #include #include #include -#include #include enum class Action { TurnOn, Toggle, TurnOff }; @@ -12,9 +11,9 @@ using Point = std::pair; /// A command struct Command { - Command(std::string const& s) + explicit Command(std::string const& s) { - const char* re = "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)"; + const char* re = R"((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"; @@ -51,7 +50,7 @@ struct Array { for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - lights_[i][j] = false; + lights_[i][j] = false; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) } } } @@ -68,13 +67,14 @@ struct Array for (unsigned j = command.bottom_left_.second; j <= command.top_right_.second; ++j) { switch (command.act_) { case Action::TurnOn: - lights_[i][j] = true; + lights_[i][j] = true; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) break; case Action::Toggle: - lights_[i][j] = !lights_[i][j]; + lights_[i][j] = // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + !lights_[i][j]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) break; case Action::TurnOff: - lights_[i][j] = false; + lights_[i][j] = false; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) break; } } @@ -82,12 +82,12 @@ struct Array } /// How many lights are on - unsigned num_on() const noexcept + [[nodiscard]] auto num_on() const noexcept -> unsigned { unsigned count = 0; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - count += lights_[i][j]; + count += lights_[i][j]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) } } @@ -100,7 +100,9 @@ struct Array std::cout << "P1\n" << N << " " << N << "\n"; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - std::cout << (lights_[i][j] ? "1" : "0"); + std::cout << (lights_[i][j] // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + ? "1" + : "0"); // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) if (j % 70 == 0) { std::cout << "\n"; } @@ -109,10 +111,10 @@ struct Array } } - bool lights_[N][N]; + bool lights_[N][N]{}; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) }; -int main(int argc, char** argv) +auto main() -> int { Array<1000> arr; for (std::string line; std::getline(std::cin, line);) { diff --git a/2015/puzzle-06-02.cc b/2015/puzzle-06-02.cc index 96a0e76..a88e440 100644 --- a/2015/puzzle-06-02.cc +++ b/2015/puzzle-06-02.cc @@ -2,16 +2,15 @@ #include #include #include -#include #include enum class Action { TurnOn, Toggle, TurnOff }; using Point = std::pair; struct Command { - Command(std::string const& s) + explicit Command(std::string const& s) { - const char* re = "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)"; + const char* re = R"((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"; @@ -47,7 +46,7 @@ struct Array { for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - lights_[i][j] = 0; + lights_[i][j] = 0; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) } } } @@ -63,14 +62,14 @@ struct Array for (unsigned j = command.bottom_left_.second; j <= command.top_right_.second; ++j) { switch (command.act_) { case Action::TurnOn: - ++lights_[i][j]; + ++lights_[i][j]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) break; case Action::Toggle: - lights_[i][j] += 2; + lights_[i][j] += 2; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) break; case Action::TurnOff: - if (lights_[i][j] > 0) { - --lights_[i][j]; + if (lights_[i][j] > 0) { // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + --lights_[i][j]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) } break; } @@ -78,12 +77,12 @@ struct Array } } - unsigned brightness() const noexcept + [[nodiscard]] auto brightness() const noexcept -> unsigned { unsigned count = 0; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - count += lights_[i][j]; + count += lights_[i][j]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) } } return count; @@ -95,23 +94,24 @@ struct Array unsigned max = 0; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - if (lights_[i][j] > max) { - max = lights_[i][j]; + if (lights_[i][j] > max) { // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + max = lights_[i][j]; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) } } } std::cout << "P2\n" << N << " " << N << "\n" << max << "\n"; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { - std::cout << lights_[i][j] << "\n"; + std::cout << lights_[i][j] // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + << "\n"; } } } - unsigned lights_[N][N]; + unsigned lights_[N][N]{}; // NOLINT(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) }; -int main(int argc, char** argv) +auto main() -> int { Array<1000> arr; for (std::string line; std::getline(std::cin, line);) { diff --git a/2015/puzzle-07-01.cc b/2015/puzzle-07-01.cc index 424de79..2a0e686 100644 --- a/2015/puzzle-07-01.cc +++ b/2015/puzzle-07-01.cc @@ -44,7 +44,7 @@ enum class Action { }; /// Pretty-print action -std::ostream& operator<<(std::ostream& os, Action act) +auto operator<<(std::ostream& os, Action act) -> std::ostream& { switch (act) { case Action::Set: @@ -74,7 +74,7 @@ using Wire = std::string; ///< Wire name (string) using Signal = std::variant; ///< Either a wire or explicit value /// Outputter for a signal -std::ostream& operator<<(std::ostream& os, Signal const& signal) +auto operator<<(std::ostream& os, Signal const& signal) -> std::ostream& { return std::visit( [&os](auto&& arg) -> std::ostream& { @@ -97,10 +97,10 @@ struct Instruction * set := signal '->' wire * not := 'NOT' set * op := 'AND' | 'OR' | 'LSHIFT' | 'RSHIFT' - * binop := signal op signal '->' wire - * instr := binop | not | set + * bin-op := signal op signal '->' wire + * instr := bin-op | not | set */ - Instruction(std::string const& s) + explicit Instruction(std::string const& s) { if (parse_bin_op(s)) { return; @@ -116,16 +116,16 @@ struct Instruction } /// Get action - Action action() const noexcept { return act_; } + [[nodiscard]] auto action() const noexcept -> Action { return act_; } /// Get the destination wire - Wire const& dest() const noexcept { return dest_; } + [[nodiscard]] auto dest() const noexcept -> Wire const& { return dest_; } /// Get the first (or only) source - Signal const& src1() const noexcept { return src1_; } + [[nodiscard]] auto src1() const noexcept -> Signal const& { return src1_; } /// Get the second source - Signal const& src2() const noexcept + [[nodiscard]] auto src2() const noexcept -> Signal const& { assert(act_ != Action::Set && act_ != Action::Not); return src2_; @@ -133,7 +133,7 @@ struct Instruction private: /// Parse a instruction. Return true if successful. - bool parse_not(std::string const& s) + auto parse_not(std::string const& s) -> bool { if (s.substr(0, 4) == "NOT ") { std::string::size_type pos = 4; @@ -146,7 +146,7 @@ private: } /// Parse a instruction. Return true if successful. - bool parse_bin_op(std::string const& s) + auto parse_bin_op(std::string const& s) -> bool { static const std::regex re("^([[:lower:][:digit:]]+) ([[:upper:]]+) " "([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); @@ -181,7 +181,7 @@ private: /// /// Also used for the latter half of parsing. ACT tells you what is /// being parsed. Returns true if parsing successful. - bool parse_set(std::string const& s, Action act = Action::Set) + auto parse_set(std::string const& s, Action act = Action::Set) -> bool { static const std::regex re("^([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); std::smatch m; @@ -196,25 +196,23 @@ private: } /// Make a Signal from a string. - Signal make_signal(std::string const& s) + static auto make_signal(std::string const& s) -> Signal { - if (std::isdigit(s[0])) { + if (std::isdigit(s[0]) == 1) { auto u = std::stoul(s, nullptr, 10); assert(u <= UINT16_MAX); - return Signal(static_cast(u)); - } - else { - return Signal(s); + return {static_cast(u)}; } + return {s}; } - Action act_; ///< Action - Wire dest_; ///< Destination wire - Signal src1_, src2_; ///< Source signals + Action act_{Action::Not}; ///< Action + Wire dest_; ///< Destination wire + Signal src1_, src2_; ///< Source signals }; /// Outputter for an instruction. -std::ostream& operator<<(std::ostream& os, Instruction const& instr) +auto operator<<(std::ostream& os, Instruction const& instr) -> std::ostream& { os << instr.action() << " " << instr.dest() << ", " << instr.src1(); if (instr.action() != Action::Set && instr.action() != Action::Not) { @@ -230,27 +228,31 @@ using Instructions = std::vector; ///< Instructions to execute struct VM { /// Add an instruction the the list we have - void add_instr(Instruction const& instr) { instrs_.push_back(instr); } + void add_instr(Instruction const& instr) { instructions_.push_back(instr); } /// Has this wire a known value? - bool has_value(Wire const& w) const noexcept { return values_.find(w) != values_.end(); } + [[nodiscard]] auto has_value(Wire const& w) const noexcept -> bool + { + return values_.find(w) != values_.end(); + } /// Has this signal a known value? - bool has_value(Signal const& s) const noexcept + [[nodiscard]] auto has_value(Signal const& s) const noexcept -> bool { - return std::visit( - Overloaded{[](Value v) { return true; }, [&](Wire const& w) { return has_value(w); }}, s); + return std::visit(Overloaded{[]([[maybe_unused]] Value v) { return true; }, + [&](Wire const& w) { return has_value(w); }}, + s); } /// Get the value on the wire - Value value(Wire const& w) const noexcept + [[nodiscard]] auto value(Wire const& w) const noexcept -> Value { assert(has_value(w)); return values_.find(w)->second; } /// Get the value of a signal - Value value(Signal const& s) const noexcept + [[nodiscard]] auto value(Signal const& s) const noexcept -> Value { return std::visit( Overloaded{[](Value v) { return v; }, [&](Wire const& w) { return value(w); }}, s); @@ -267,15 +269,17 @@ struct VM void value(Signal const& s, Value v) { std::visit( - Overloaded{[v](Value v2) { assert(v == v2); }, [&, v](Wire const& w) { value(w, v); }}, s); + Overloaded{[v](Value v2) { assert(v == v2); }, // NOLINT(bugprone-lambda-function-name) + [&, v](Wire const& w) { value(w, v); }}, // NOLINT(bugprone-lambda-function-name) + s); // NOLINT(bugprone-lambda-function-name) } /// Execute the instructions. Returns true if we have updated some wire /// values. - bool execute() + auto execute() -> bool { bool done_anything = false; - for (auto const& instr : instrs_) { + for (auto const& instr : instructions_) { done_anything |= execute_instr(instr); } @@ -290,13 +294,13 @@ private: * An instruction may not be executed if the incoming signals have not been * set yet. */ - bool execute_instr(Instruction const& instr) + auto execute_instr(Instruction const& instr) -> bool { std::cout << instr << " # "; - // First of all check there is something to do - i.e. that the destination + // First check there is something to do - i.e. that the destination // register has not been set already. - Wire dest = instr.dest(); + auto const& dest{instr.dest()}; if (has_value(dest)) { std::cout << "already has value: " << dest << " = " << value(dest) << "\n"; return false; @@ -325,10 +329,10 @@ private: * \param fn How to modify the source value to the dest. * \return True if we executed the function. */ - bool execute_single_src(Instruction const& instr, std::function fn) + auto execute_single_src(Instruction const& instr, const std::function& fn) -> bool { - Wire dest = instr.dest(); - Signal src = instr.src1(); + auto const& dest{instr.dest()}; + Signal const& src{instr.src1()}; if (has_value(src)) { value(dest, fn(value(src))); std::cout << "setting wire to: " << dest << " = " << value(dest) << "\n"; @@ -344,11 +348,12 @@ private: * \param fn How to modify the source values to the dest. * \return True if we executed the function. */ - bool execute_double_src(Instruction const& instr, std::function fn) + auto execute_double_src(Instruction const& instr, const std::function& fn) + -> bool { - Wire dest = instr.dest(); - Signal src1 = instr.src1(); - Signal src2 = instr.src2(); + auto const& dest{instr.dest()}; + auto const& src1{instr.src1()}; + auto const& src2{instr.src2()}; if (has_value(src1) && has_value(src2)) { value(dest, fn(value(src1), value(src2))); std::cout << "setting wire to: " << dest << " = " << value(dest) << "\n"; @@ -360,10 +365,10 @@ private: } ValueMap values_; - Instructions instrs_; + Instructions instructions_; }; -int main(int argc, char** argv) +auto main() -> int { VM vm; diff --git a/2015/puzzle-07-02.cc b/2015/puzzle-07-02.cc index 424de79..0c844a3 100644 --- a/2015/puzzle-07-02.cc +++ b/2015/puzzle-07-02.cc @@ -44,7 +44,7 @@ enum class Action { }; /// Pretty-print action -std::ostream& operator<<(std::ostream& os, Action act) +auto operator<<(std::ostream& os, Action act) -> std::ostream& { switch (act) { case Action::Set: @@ -74,7 +74,7 @@ using Wire = std::string; ///< Wire name (string) using Signal = std::variant; ///< Either a wire or explicit value /// Outputter for a signal -std::ostream& operator<<(std::ostream& os, Signal const& signal) +auto operator<<(std::ostream& os, Signal const& signal) -> std::ostream& { return std::visit( [&os](auto&& arg) -> std::ostream& { @@ -100,7 +100,7 @@ struct Instruction * binop := signal op signal '->' wire * instr := binop | not | set */ - Instruction(std::string const& s) + explicit Instruction(std::string const& s) { if (parse_bin_op(s)) { return; @@ -116,16 +116,16 @@ struct Instruction } /// Get action - Action action() const noexcept { return act_; } + [[nodiscard]] auto action() const noexcept -> Action { return act_; } /// Get the destination wire - Wire const& dest() const noexcept { return dest_; } + [[nodiscard]] auto dest() const noexcept -> Wire const& { return dest_; } /// Get the first (or only) source - Signal const& src1() const noexcept { return src1_; } + [[nodiscard]] auto src1() const noexcept -> Signal const& { return src1_; } /// Get the second source - Signal const& src2() const noexcept + [[nodiscard]] auto src2() const noexcept -> Signal const& { assert(act_ != Action::Set && act_ != Action::Not); return src2_; @@ -133,7 +133,7 @@ struct Instruction private: /// Parse a instruction. Return true if successful. - bool parse_not(std::string const& s) + auto parse_not(std::string const& s) -> bool { if (s.substr(0, 4) == "NOT ") { std::string::size_type pos = 4; @@ -146,7 +146,7 @@ private: } /// Parse a instruction. Return true if successful. - bool parse_bin_op(std::string const& s) + auto parse_bin_op(std::string const& s) -> bool { static const std::regex re("^([[:lower:][:digit:]]+) ([[:upper:]]+) " "([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); @@ -181,7 +181,7 @@ private: /// /// Also used for the latter half of parsing. ACT tells you what is /// being parsed. Returns true if parsing successful. - bool parse_set(std::string const& s, Action act = Action::Set) + auto parse_set(std::string const& s, Action act = Action::Set) -> bool { static const std::regex re("^([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); std::smatch m; @@ -196,25 +196,23 @@ private: } /// Make a Signal from a string. - Signal make_signal(std::string const& s) + static auto make_signal(std::string const& s) -> Signal { - if (std::isdigit(s[0])) { + if (std::isdigit(s[0]) == 1) { auto u = std::stoul(s, nullptr, 10); assert(u <= UINT16_MAX); - return Signal(static_cast(u)); - } - else { - return Signal(s); + return {static_cast(u)}; } + return {s}; } - Action act_; ///< Action - Wire dest_; ///< Destination wire - Signal src1_, src2_; ///< Source signals + Action act_{Action::Not}; ///< Action + Wire dest_; ///< Destination wire + Signal src1_, src2_; ///< Source signals }; /// Outputter for an instruction. -std::ostream& operator<<(std::ostream& os, Instruction const& instr) +auto operator<<(std::ostream& os, Instruction const& instr) -> std::ostream& { os << instr.action() << " " << instr.dest() << ", " << instr.src1(); if (instr.action() != Action::Set && instr.action() != Action::Not) { @@ -230,27 +228,30 @@ using Instructions = std::vector; ///< Instructions to execute struct VM { /// Add an instruction the the list we have - void add_instr(Instruction const& instr) { instrs_.push_back(instr); } + void add_instr(Instruction const& instr) { instructions_.push_back(instr); } /// Has this wire a known value? - bool has_value(Wire const& w) const noexcept { return values_.find(w) != values_.end(); } + [[nodiscard]] auto has_value(Wire const& w) const noexcept -> bool + { + return values_.find(w) != values_.end(); + } /// Has this signal a known value? - bool has_value(Signal const& s) const noexcept + [[nodiscard]] auto has_value(Signal const& s) const noexcept -> bool { return std::visit( - Overloaded{[](Value v) { return true; }, [&](Wire const& w) { return has_value(w); }}, s); + Overloaded{[](Value /*v*/) { return true; }, [&](Wire const& w) { return has_value(w); }}, s); } /// Get the value on the wire - Value value(Wire const& w) const noexcept + [[nodiscard]] auto value(Wire const& w) const noexcept -> Value { assert(has_value(w)); return values_.find(w)->second; } /// Get the value of a signal - Value value(Signal const& s) const noexcept + [[nodiscard]] auto value(Signal const& s) const noexcept -> Value { return std::visit( Overloaded{[](Value v) { return v; }, [&](Wire const& w) { return value(w); }}, s); @@ -267,15 +268,17 @@ struct VM void value(Signal const& s, Value v) { std::visit( - Overloaded{[v](Value v2) { assert(v == v2); }, [&, v](Wire const& w) { value(w, v); }}, s); + Overloaded{[v](Value v2) { assert(v == v2); }, + [&, v](Wire const& w) { value(w, v); }}, // NOLINT(bugprone-lambda-function-name) + s); // NOLINT(bugprone-lambda-function-name) } /// Execute the instructions. Returns true if we have updated some wire /// values. - bool execute() + auto execute() -> bool { bool done_anything = false; - for (auto const& instr : instrs_) { + for (auto const& instr : instructions_) { done_anything |= execute_instr(instr); } @@ -290,13 +293,13 @@ private: * An instruction may not be executed if the incoming signals have not been * set yet. */ - bool execute_instr(Instruction const& instr) + auto execute_instr(Instruction const& instr) -> bool { std::cout << instr << " # "; // First of all check there is something to do - i.e. that the destination // register has not been set already. - Wire dest = instr.dest(); + auto const& dest = instr.dest(); if (has_value(dest)) { std::cout << "already has value: " << dest << " = " << value(dest) << "\n"; return false; @@ -325,10 +328,10 @@ private: * \param fn How to modify the source value to the dest. * \return True if we executed the function. */ - bool execute_single_src(Instruction const& instr, std::function fn) + auto execute_single_src(Instruction const& instr, const std::function& fn) -> bool { - Wire dest = instr.dest(); - Signal src = instr.src1(); + const Wire& dest = instr.dest(); + const Signal& src = instr.src1(); if (has_value(src)) { value(dest, fn(value(src))); std::cout << "setting wire to: " << dest << " = " << value(dest) << "\n"; @@ -344,11 +347,12 @@ private: * \param fn How to modify the source values to the dest. * \return True if we executed the function. */ - bool execute_double_src(Instruction const& instr, std::function fn) + auto execute_double_src(Instruction const& instr, const std::function& fn) + -> bool { - Wire dest = instr.dest(); - Signal src1 = instr.src1(); - Signal src2 = instr.src2(); + const Wire& dest = instr.dest(); + const Signal& src1 = instr.src1(); + const Signal& src2 = instr.src2(); if (has_value(src1) && has_value(src2)) { value(dest, fn(value(src1), value(src2))); std::cout << "setting wire to: " << dest << " = " << value(dest) << "\n"; @@ -360,10 +364,10 @@ private: } ValueMap values_; - Instructions instrs_; + Instructions instructions_; }; -int main(int argc, char** argv) +auto main() -> int { VM vm; diff --git a/2015/puzzle-08-01.cc b/2015/puzzle-08-01.cc index 44025ef..163cc43 100644 --- a/2015/puzzle-08-01.cc +++ b/2015/puzzle-08-01.cc @@ -1,14 +1,11 @@ #include #include #include -#include -#include #include -#include enum class State { Begin, Normal, Escape, Hex1, Hex2, End }; -std::string unescape(std::string const& s) +auto unescape(std::string const& s) -> std::string { std::string unescaped; static const std::string hex = "0123456789abcdef0123456789ABCDEF"; @@ -56,7 +53,7 @@ std::string unescape(std::string const& s) auto idx = hex.find(c); assert(idx != std::string::npos); byte = (byte << 4) + (idx & 0xf); - unescaped += (char)byte; + unescaped += static_cast(byte); state = State::Normal; break; } @@ -67,7 +64,7 @@ std::string unescape(std::string const& s) return unescaped; } -int main(int argc, char** argv) +auto main() -> int { unsigned len = 0; diff --git a/2015/puzzle-08-02.cc b/2015/puzzle-08-02.cc index 7119819..f708505 100644 --- a/2015/puzzle-08-02.cc +++ b/2015/puzzle-08-02.cc @@ -1,12 +1,7 @@ -#include #include #include -#include -#include -#include -#include -std::string escape(std::string const& s) +auto escape(std::string const& s) -> std::string { std::string escaped; escaped += '"'; @@ -20,7 +15,7 @@ std::string escape(std::string const& s) return escaped; } -int main(int argc, char** argv) +auto main() -> int { unsigned len = 0; diff --git a/2015/puzzle-09-01.cc b/2015/puzzle-09-01.cc index c154c88..69948dd 100644 --- a/2015/puzzle-09-01.cc +++ b/2015/puzzle-09-01.cc @@ -21,8 +21,7 @@ struct Graph std::smatch m; if (!std::regex_search(s, m, re)) { std::cout << "Failed to match: " << s << "\n"; - assert(false); - return; + abort(); } auto n1 = m.str(1); auto n2 = m.str(2); @@ -37,7 +36,7 @@ struct Graph std::cout << n1 << " <-> " << n2 << " weight: " << w << "\n"; } - Weight solve_tsp() const + [[nodiscard]] auto solve_tsp() const -> Weight { Weight min_weight = ~0U; std::vector nodes(nodes_.begin(), nodes_.end()); @@ -67,7 +66,7 @@ struct Graph Edges weights_; }; -int main(int argc, char** argv) +auto main() -> int { Graph g; diff --git a/2015/puzzle-09-02.cc b/2015/puzzle-09-02.cc index c4567d2..2f173ef 100644 --- a/2015/puzzle-09-02.cc +++ b/2015/puzzle-09-02.cc @@ -21,8 +21,7 @@ struct Graph std::smatch m; if (!std::regex_search(s, m, re)) { std::cout << "Failed to match: " << s << "\n"; - assert(false); - return; + abort(); } auto n1 = m.str(1); auto n2 = m.str(2); @@ -37,7 +36,7 @@ struct Graph std::cout << n1 << " <-> " << n2 << " weight: " << w << "\n"; } - Weight solve_tsp() const + [[nodiscard]] auto solve_tsp() const -> Weight { Weight min_weight = ~0U; visit_all_perms([&min_weight](Weight w) { @@ -49,7 +48,7 @@ struct Graph return min_weight; } - Weight solve_max_tsp() const + [[nodiscard]] auto solve_max_tsp() const -> Weight { Weight max_weight = 0; visit_all_perms([&max_weight](Weight w) { @@ -86,7 +85,7 @@ private: Edges weights_; }; -int main(int argc, char** argv) +auto main() -> int { Graph g; diff --git a/2015/puzzle-10-01.cc b/2015/puzzle-10-01.cc index aa004fa..c0072cb 100644 --- a/2015/puzzle-10-01.cc +++ b/2015/puzzle-10-01.cc @@ -1,13 +1,8 @@ -#include #include #include -#include -#include -#include #include -#include -std::string look_and_say(std::string const& s) +auto look_and_say(std::string const& s) -> std::string { std::string result; for (std::string::size_type i = 0; i < s.length();) { @@ -23,7 +18,7 @@ std::string look_and_say(std::string const& s) return result; } -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { std::cout << "Application 0, length = " << line.length() << ": " << line << "\n"; diff --git a/2015/puzzle-10-02.cc b/2015/puzzle-10-02.cc index d64e749..34c8d4a 100644 --- a/2015/puzzle-10-02.cc +++ b/2015/puzzle-10-02.cc @@ -1,13 +1,8 @@ -#include #include #include -#include -#include -#include #include -#include -std::string look_and_say(std::string const& s) +auto look_and_say(std::string const& s) -> std::string { std::string result; for (std::string::size_type i = 0; i < s.length();) { @@ -23,7 +18,7 @@ std::string look_and_say(std::string const& s) return result; } -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { std::cout << "Application 0, length = " << line.length() << ": " << line << "\n"; diff --git a/2015/puzzle-11-01.cc b/2015/puzzle-11-01.cc index fbe8e53..6edaa36 100644 --- a/2015/puzzle-11-01.cc +++ b/2015/puzzle-11-01.cc @@ -1,13 +1,9 @@ #include #include #include -#include -#include -#include #include -#include -bool illegal_char(char c) { return c == 'i' || c == 'l' || c == 'o'; } +auto illegal_char(char c) -> bool { return c == 'i' || c == 'l' || c == 'o'; } void pre_advance_password(std::string& s) { @@ -43,7 +39,7 @@ void advance_password(std::string& s) } } -bool valid_password(std::string const& s) +auto valid_password(std::string const& s) -> bool { unsigned double_count = 0; bool run = false; @@ -63,7 +59,7 @@ bool valid_password(std::string const& s) return double_count >= 2 && run; } -std::string next_password(std::string const& s) +auto next_password(std::string const& s) -> std::string { std::string result = s; pre_advance_password(result); @@ -73,7 +69,7 @@ std::string next_password(std::string const& s) return result; } -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { std::string next = next_password(line); diff --git a/2015/puzzle-11-02.cc b/2015/puzzle-11-02.cc index e5ec028..36f5c54 100644 --- a/2015/puzzle-11-02.cc +++ b/2015/puzzle-11-02.cc @@ -1,13 +1,9 @@ #include #include #include -#include -#include -#include #include -#include -bool illegal_char(char c) { return c == 'i' || c == 'l' || c == 'o'; } +auto illegal_char(char c) -> bool { return c == 'i' || c == 'l' || c == 'o'; } void pre_advance_password(std::string& s) { @@ -43,7 +39,7 @@ void advance_password(std::string& s) } } -bool valid_password(std::string const& s) +auto valid_password(std::string const& s) -> bool { unsigned double_count = 0; bool run = false; @@ -63,7 +59,7 @@ bool valid_password(std::string const& s) return double_count >= 2 && run; } -std::string next_password(std::string const& s) +auto next_password(std::string const& s) -> std::string { std::string result = s; pre_advance_password(result); @@ -73,7 +69,7 @@ std::string next_password(std::string const& s) return result; } -int main(int argc, char** argv) +auto main() -> int { for (std::string line; std::getline(std::cin, line);) { std::string next = next_password(line); diff --git a/2015/puzzle-12-01.cc b/2015/puzzle-12-01.cc index 81131d4..71ef565 100644 --- a/2015/puzzle-12-01.cc +++ b/2015/puzzle-12-01.cc @@ -1,13 +1,9 @@ -#include #include #include -#include #include -#include #include -#include -int parse_numbers(std::string const& s) +auto parse_numbers(std::string const& s) -> int { static const std::regex re("-?\\d+"); std::string left = s; @@ -21,7 +17,7 @@ int parse_numbers(std::string const& s) return acc; } -int main(int argc, char** argv) +auto main() -> int { int acc = 0; for (std::string line; std::getline(std::cin, line);) { diff --git a/2015/puzzle-12-02.cc b/2015/puzzle-12-02.cc index 660c18d..09c7bd1 100644 --- a/2015/puzzle-12-02.cc +++ b/2015/puzzle-12-02.cc @@ -2,23 +2,15 @@ #include #include #include -#include -#include -#include #include -#include -int do_parse(std::string const& s, std::string::size_type& pos) +auto do_parse(std::string const& s, std::string::size_type& pos) -> int { int result = 0; bool ignore = false; while (pos < s.size()) { - if (s[pos] == '{') { - ++pos; - result += do_parse(s, pos); - } - else if (s[pos] == '[') { + if (s[pos] == '{' || s[pos] == '[') { ++pos; result += do_parse(s, pos); } @@ -40,7 +32,7 @@ int do_parse(std::string const& s, std::string::size_type& pos) } pos = e + 1; } - else if (std::isdigit(s[pos]) || s[pos] == '-') { + else if (std::isdigit(s[pos]) == 1 || s[pos] == '-') { std::size_t len = 0; result += std::stoi(s.substr(pos), &len); pos += len; @@ -54,7 +46,7 @@ int do_parse(std::string const& s, std::string::size_type& pos) return result; } -int parse_numbers(std::string const& s) +auto parse_numbers(std::string const& s) -> int { std::string::size_type pos = 0; int result = do_parse(s, pos); @@ -62,7 +54,7 @@ int parse_numbers(std::string const& s) return result; } -int main(int argc, char** argv) +auto main() -> int { int acc = 0; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-08-01.cc b/2020/puzzle-08-01.cc index a33077d..6c7ec74 100644 --- a/2020/puzzle-08-01.cc +++ b/2020/puzzle-08-01.cc @@ -89,16 +89,16 @@ struct VM void add_instruction(Instruction const& i) { std::cout << i << "\n"; - instrs_.push_back(i); + instructions_.push_back(i); } void execute() { - std::vector seen(instrs_.size(), false); + std::vector seen(instructions_.size(), false); while (!seen[pc_]) { - assert(pc_ < instrs_.size()); + assert(pc_ < instructions_.size()); seen[pc_] = true; - execute(instrs_[pc_]); + execute(instructions_[pc_]); ++pc_; } std::cout << "PC seen before: " << pc_ << "\n"; @@ -127,7 +127,7 @@ private: std::cout << "\n"; } - Instructions instrs_; + Instructions instructions_; std::size_t pc_; Value acc_; }; diff --git a/2020/puzzle-08-02.cc b/2020/puzzle-08-02.cc index 101e088..3979d78 100644 --- a/2020/puzzle-08-02.cc +++ b/2020/puzzle-08-02.cc @@ -104,20 +104,20 @@ struct VM void add_instruction(Instruction const& i) { std::cout << i << "\n"; - instrs_.push_back(i); + instructions_.push_back(i); } bool execute() { - std::vector seen(instrs_.size(), false); + std::vector seen(instructions_.size(), false); acc_ = 0; pc_ = 0; while (!seen[pc_]) { - assert(pc_ < instrs_.size()); + assert(pc_ < instructions_.size()); seen[pc_] = true; - execute(instrs_[pc_]); + execute(instructions_[pc_]); ++pc_; - if (pc_ == instrs_.size()) { + if (pc_ == instructions_.size()) { std::cout << "Terminated\n"; return true; } @@ -128,13 +128,13 @@ struct VM void find_fix() { - for (std::size_t pos = 0; pos < instrs_.size(); ++pos) { - if (instrs_[pos].flip_jmp()) { + for (std::size_t pos = 0; pos < instructions_.size(); ++pos) { + if (instructions_[pos].flip_jmp()) { if (execute()) { - std::cout << "Success at instruction " << pos << ": " << instrs_[pos] << "\n"; + std::cout << "Success at instruction " << pos << ": " << instructions_[pos] << "\n"; return; } - instrs_[pos].flip_jmp(); + instructions_[pos].flip_jmp(); } } } @@ -162,7 +162,7 @@ private: std::cout << "\n"; } - Instructions instrs_; + Instructions instructions_; std::size_t pc_; Value acc_; };